Friendship of Frog
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 5375 Accepted Submission(s): 3129
Problem 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 2ndfrog, 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.
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.
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
Source
/*
*@Author: GuoJinlong
*@Language: C++
*/
//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<list>
#include<set>
#include<iomanip>
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cassert>
#include<sstream>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
typedef long long ll;
#define ull unsigned long long
#define ls (rt<<1)
#define rs (rt<<1|1)
#define mid (l+r)/2
#define mms(x, y) memset(x, y, sizeof x)
#define over(i,s,t) for(register long long i=s;i<=t;++i)
#define lver(i,t,s) for(register long long i=t;i>=s;--i)
const int MAXN = 305;
const int INF = 0x3f3f3f3f;
const int N=5e4+7;
const int maxn=1e5+5;
const double EPS=1e-10;
const double Pi=3.1415926535897;
//inline double max(double a,double b){
// return a>b?a:b;
//}
//inline double min(double a,double b){
// return a<b?a:b;
//}
int xd[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int yd[8] = {1, 0, -1, 0, -1, 1, -1, 1};
//fastPow
int fastPow(int x,int n){
if(n==1) return x;
int tmp=fastPow(x,n/2); //fenzi
if(n%2==1) return tmp*tmp*x;
else return tmp*tmp;
}
ll gcd(ll m, ll n){return n == 0 ? m : gcd(n, m%n);}
ll lcm(ll m, ll n){return m*n / gcd(m, n);}
ll pows(ll base, ll power,ll mod){ll result=1;while(power>0){if(power&1){result=result*base%mod;}power>>=1;base=(base*base)%mod;}return result;}
ll poww(ll base, ll power){ll result=1;while(power>0){if(power&1){result=result*base;}power>>=1;base=(base*base);}return result;}
//dijsktra
//start
//const int MAX= 10020;
//struct node {
// int x,to,next,w;
// bool operator <(const node &a) const{
// return this->w>a.w;
// }
//}G[MAX<<1];
//int cnt;
//int dis[MAX];
//int vis[MAX];
//int head[MAX];
//
//
//int m,n;
//int b[10020];
//
//
//void add(int u,int v,int w){
// G[++cnt].to=v;
// G[cnt].w=w;
// G[cnt].next=head[u];
// head[u]=cnt;
//}
//int dijkstra(int s){
// mms(dis,INF);
// dis[s]=0;
// priority_queue<node> p;
// node t;
// t.x=s;
// t.w=0;
// p.push(t);
// while (!p.empty()) {
// node u=p.top();
// p.pop();
// int v=u.x;
// if(dis[v]!=u.w) continue;
// for(int i=head[v];i;i=G[i].next){
// int to=G[i].to;
// if(dis[to]>dis[v]+G[i].w){
// dis[to]=dis[v]+G[i].w;
// t.x=to;
// t.w=dis[to];
// p.push(t);
// }
//
// }
// }
// int m1=-1;
// int m2=-1;
// dis[s]=-3;
// for(int i=1;i<=n;i++){
// if(dis[i]==INF) continue;
// if(dis[i]>m1){
// m2=m1;
// m1=dis[i];
// }
// else if(dis[i]>m2){
// m2=dis[i];
// }
// }
// if(m1!=-1&&m2!=-1) return m1+m2;
// return -1;
//
//}
//
//
//int main(){
// int t;
// cin>>t;
// while (t--) {
// cin>>n>>m;
// int u,v,w;
//
// for(int i=1;i<=m;i++){
// cin>>u>>v>>w;
// add(u,v,w);
// add(v,u,w);
// }
// int ans=0;
// int cnt=0;
// for(int i=1;i<=n;i++){
// ans=max(ans,dijkstra(i));
// }
// cout<<ans<<endl;
// }
//}
//end
//e_cheak
//int vis[1000010];
//int prime[100010];
//int e_cheak(int n){ //k为2-n素数的个数
// for(int i=0;i<=n;i++){
// vis[i]=0;
// }
// for(int i=2;i*i<=n;i++){
// if(!vis[i]){
// for(int j=i*i;j<=n;j+=i){
// vis[j]=1;
// }
// }
// }
// int k=0;
// for(int i=2;i<=n;i++){
// if(!vis[i])
// prime[k++]=i; //统计素数
// }
// return k;
//}
//
进制转化 x转化为y进制
//string work(int x,int y){
// string str="";
// while(x){
// if(x%y>=10) str+=x%y+'A'-10;
// else str+=x%y+'0';
// x/=y;
// }
// reverse(str.begin(),str.end());
// return str;
//}
//
//
//const int MAX=1000005;
//char str[MAX];
//char pattern[MAX];
//int cnt;
//int Next[MAX];
//void getnext(string p,int plen){
// Next[0]=0;
// Next[1]=0;
// for(int i=1;i<plen;i++){
// int j=Next[i];
// while (j&&p[i]!=p[j]) {
// j=Next[j];
// }
// Next[i+1]=(p[i]==p[j])?j+1:0;
// }
//}
//int kmp(string s,string p){
// int last=-1;
// int slen=s.length();
// int plen=p.length();
// getnext(p,plen);
// int j=0;
// for(int i=0;i<slen;i++){
// while (j&&s[i]!=p[j]) {
// j=Next[j];
// }
// if(s[i]==p[j]) j++;
// if(j==plen){
// //start
// return 1;
// //end
// }
// }
// return 0;
//}
//int main(){
// int n;
// cin>>n;
// string t;
// string s="";
// for(int i=2;i<=16;i++){
// s="";
// s+=work(n,i);
// if(kmp(s,t))
// {
// cout<<"yes";
// return 0;
// }
// }
// cout<<"no"<<endl;
// return 0;
//}
//string s1,s2;
//int main(){
// cin>>s1;
// s2="cocacola";
// int ans;
// for(int i=0;i<s1.length();i++){
// if(s1[i]!=s2[i])
// ans++;
// }
// cout<<ceil(ans/2);
//}
string s;
int t;
int main(){
cin>>t;
int num=1;
while (t--) {
cin>>s;
int ans=INF;
for(int i=0;i<s.size();i++){
for(int j=1;j<=26&&i-j>=0;j++){ //出现相同字符最多要26位
if(s[i]==s[i-j]){
ans=min(ans,j);
break;//已经找到最近的 没必要继续进行了
}
}
}
if(ans==INF) cout<<"Case #"<<num++<<": "<<-1<<endl;
else cout<<"Case #"<<num++<<": "<<ans<<endl;
}
}