Hdoj.5578 Friendship of Frog【字符串,暴力】 2015/12/04

来源:互联网 发布:mac如何打开msg文件 编辑:程序博客网 时间:2024/06/11 12:59

Friendship of Frog

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 181    Accepted Submission(s): 130


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. the1st 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", wherex 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
2abcecbaabc
 

Sample Output
Case #1: 2Case #2: -1
 

Source
2015ACM/ICPC亚洲区上海站-重现赛(感谢华东理工)  
题意:求两个相同字母间的最小距离,若不存在两个相同的字母,输出-1.
直接暴力求解,从某一字符往后面再找26个字符即可
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int main(){    char s[1010];    int t,ans=1;    cin>>t;    while(t--){        scanf("%s",s);        printf("Case #%d: ",ans++);        int l = strlen(s);        int dis = l*2;        for( int i = 0 ; i < l ; ++i ){            for( int j = 1 ; j <= 26 && i+j < l ; ++j ){                if( s[i] == s[i+j] ){                    if( j < dis )                        dis = j;                    break;                }            }            if( dis == 1 )                break;        }        if( dis == 2*l )            printf("-1\n");        else printf("%d\n",dis);    }    return 0;}


0 0