HDU5578 Friendship of Frog

    xiaoxiao2021-11-30  35

    Friendship of Frog

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1463    Accepted Submission(s): 960 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  N1th  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.   1T50 .  for 80% data,  1N100 .  for 100% data,  1N1000 .  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 2015ACM/ICPC亚洲区上海站-重现赛(感谢华东理工)

    题意:给一个长度不超过1000的字符串,求相同字符最小间隔,没有相同字符输出-1

    #include <iostream> #include <stdio.h> #include <string> #include <cstring> #include <algorithm> #include <cmath> #define ll __int64 using namespace std; char s[2000]; int main() { int T; scanf("%d",&T); int ca=1; while(T--) { scanf("%s",s); int len=strlen(s); int ans=999999; int flag; for(int i=0;i<26;i++) { int f='a'+i; flag=0; int pos; int dis=999999; for(int j=0;j<len;j++) { if(f==s[j]) { if(flag==0)//记录该字符第一次出现的位置 { pos=j; flag=1; continue; } dis=min(dis,j-pos); pos=j; } } ans=min(dis,ans); } if(ans==999999) ans=-1; printf("Case #%d: %d\n",ca++,ans); } return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-679132.html

    最新回复(0)