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
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
Sample Output
. 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.
The closest friends are a pair of friends with the minimum distance. Help us find that distance.
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.
⋅
2 abcecba abc
Case #1: 2 Case #2: -1
这个就是求两个相同字母之间的最小距离,如果不纯在相同字母输出-1
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
using namespace std;
char s[1100];
int main()
{
ios::sync_with_stdio(false);
int t, n, i, j, sum, x = 1;
int m;
cin>>t;
while(t--)
{
cin>>s;
n = strlen(s);
m = inf;
for(i = 0;i < n;i++)
{
sum = 0;
for(j = i + 1;j < n;j++)
{
sum++;
if(s[i] == s[j])
{
m = min(m,sum);
break;
}
}
}
cout<<"Case #"<<x<<": ";
x++;
if(m < inf)
cout<<m<<endl;
else
cout<<-1<<endl;
}
return 0;
}