Friendship of Frog
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
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.
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
题意:给你一个字符串,找最近的相同字符
解题思路:最多才1000个字符,既然是签到题,就不要多想了,暴力上就可以了
对于第i个字符,往后找相同字符,不断更新Min值就ok了
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
#define MAX(a,b) ((a)>(b)?(a):(b))
using namespace std;
const int N = 1005;
const int M = 2010;
const int inf = 2147483647;
const int mod = 2009;
char s[N];
int main()
{
int t,i,j,Min,p=1;
scanf("%d",&t);
while(t--)
{
Min=inf;
scanf("%s",s);
for(i=0;s[i]!='\0';i++)
for(j=i+1;s[j]!='\0';j++)
if(s[i]==s[j])
Min=min(Min,j-i);
printf("Case #%d: ",p++);
if(Min!=inf)
printf("%d\n",Min);
else
puts("-1");
}
return 0;
}
菜鸟成长记