hdu 5578 Friendship of Frog【水题】
2016-07-22 18:27
537 查看
Friendship of Frog
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 909 Accepted Submission(s): 570
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 2ndfrog, 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
2
abcecba
abc
Sample Output
Case #1: 2
Case #2: -1
Source
2015ACM/ICPC亚洲区上海站-重现赛(感谢华东理工)
题目大意:找两个相同字母最近的距离。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
char a[5000000];
int main()
{
int t;
int kase=0;
scanf("%d",&t);
while(t--)
{
scanf("%s",&a);
int n=strlen(a);
int output=0x3f3f3f3f;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
output=min(output,(j-i));
break;
}
}
}
if(output!=0x3f3f3f3f)
printf("Case #%d: %d\n",++kase,output);
else printf("Case #%d: -1\n",++kase);
}
}
相关文章推荐
- 【HDU 5366】The mook jong 详解
- 【HDU 2136】Largest prime factor 详细图解
- 【HDU 1568】Fibonacci 数学公式 详解
- HDU 1568
- HDU1290
- HDU1568(Fobonacci公式)
- HDU ACM Step 2.2.2 Joseph(约瑟夫环问题)
- HDU 1405
- HDU 1297
- hdu 1205
- hdu 2087
- hdu 1016
- HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
- HDU 5592 ZYB's Premutation 线段树(查找动态区间第K大)
- HDU 5240 Exam (好水的题)
- HDU5237 Base64 大模拟
- HDU 1000
- HDU 1001
- HDU 1016 Prime Ring Problem
- HDU 1017 A Mathematical Curiosity