HDU 5578 Friendship of Frog 水题

时间:2022-02-08 05:00:24

Friendship of Frog

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5578

Description

N frogs from different countries are standing in a line. Each country is represented by a lowercase letter. The distance between adjacent frogs (e.g. the 1st and the 2nd frog, the N−1th and the Nth frog, etc) are exactly 1. Two frogs are friends if they come from the same country.

The closest friends are a pair of friends with the minimum distance. Help us find that distance.

Input

First line contains an integer T, which indicates the number of test cases.

Every test case only contains a string with length N, and the ith character of the string indicates the country of ith frogs.

⋅ 1≤T≤50.

⋅ for 80% data, 1≤N≤100.

⋅ for 100% data, 1≤N≤1000.

⋅ the string only contains lowercase letters.

Output

For every test case, you should output "Case #x: y", where x indicates the case number and counts from 1 and y is the result. If there are no frogs in same country, output −1 instead.

Sample Input

2
abcecba
abc

Sample Output

Case #1: 2
Case #2: -1

HINT

题意

给你一个字符串,然后问你相距最近的相同字符串的长度是多少

题解:

水题,暴力n^2去扫就好了

代码:

#include<iostream>
#include<stdio.h>
using namespace std; string s;
int main()
{
int t;scanf("%d",&t);
for(int cas=;cas<=t;cas++)
{
cin>>s;
int n=s.size();
int ans = ;
for(int i=;i<n;i++)
for(int j=i+;j<n;j++)
if(s[i]==s[j])
ans = min(ans,j-i);
if(ans==)ans=-;
printf("Case #%d: %d\n",cas,ans);
}
}