将同一个country的frog放在一起,按照index从小到大排序。因此minimum distance一定出现在相邻的两个frog中。
#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<string>
#include<cmath>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
using namespace std;
//hdu 5578
const int maxn=1010;
const int INF=0x3f3f3f3f;
int T;
int N;
int ans;
class frog
{
public:
int country;
int idx;
public:
frog()
{
country=0;
idx=0;
}
frog(int c,int id)
{
country=c;
idx=id;
}
bool operator <(const frog a) const
{
return idx<a.idx;
}
};
vector<frog>mp[27];
int main()
{
freopen("input.txt","r",stdin);
scanf("%d",&T);
for(int ca=1;ca<=T;ca++)
{
char str[maxn];
memset(mp,0,sizeof(mp));
scanf("%s",&str);
for(int i=0;i<strlen(str);i++)
{
mp[str[i]-'a'].push_back(frog(str[i]-'a',i+1));
}
ans=INF;
for(int i=0;i<26;i++)
{
sort(mp[i].begin(),mp[i].end());
for(int j=1;j<mp[i].size();j++)
{
ans=min(ans,mp[i][j].idx-mp[i][j-1].idx);
}
}
if(ans==INF) ans=-1;
printf("Case #%d: %d\n",ca,ans);
}
}