Friendship of FrogTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 14 Accepted Submission(s): 12
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 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
Sample Output
|
题意:给定一个字符串,让你找到相同字母(任意的)的最小距离,没有输出-1。
AC代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#define INF 0x3f3f3f
#define eps 1e-8
#define MAXN (1000+10)
#define MAXM (100000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
char str[1010];
int pre[30];
int main()
{
int t, kcase = 1; Ri(t);
W(t)
{
Rs(str);
int len = strlen(str);
CLR(pre, -1);
int ans = INF;
for(int i = 0; i < len; i++)
{
if(pre[str[i]-'a'] != -1)
ans = min(ans, i - pre[str[i]-'a']);
pre[str[i]-'a'] = i;
}
if(ans == INF)
ans = -1;
printf("Case #%d: %d\n", kcase++, ans);
}
return 0;
}