Friendship of Frog

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

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 −1instead.
 
Sample Input
2 abcecba abc
 
Sample Output
Case #1: 2 Case #2: -1

题解:水。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long LL;
#define T_T while(T--)
#define SI(x) scanf("%d",&x)
#define mem(x,y) memset(x,y,sizeof(x))
const int MAXN=1010;
char s[MAXN];
int a[30];
int main(){
int T,len,kase=0;
SI(T);
T_T{
scanf("%s",s);
len=strlen(s);
mem(a,0);
int ans=INF;
for(int i=0;i<len;i++){
if(!a[s[i]-'a'])a[s[i]-'a']=i;
else ans=min(ans,i-a[s[i]-'a']),a[s[i]-'a']=i;
}
if(ans==INF)ans=-1;
printf("Case #%d: %d\n",++kase,ans);
}
return 0;
}

  

Friendship of Frog(水题)的更多相关文章

  1. HDU 5578 Friendship of Frog 水题

    Friendship of Frog Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.ph ...

  2. HDU 4584 Building bridges (水题)

    Building bridges Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) ...

  3. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  4. ACM :漫漫上学路 -DP -水题

    CSU 1772 漫漫上学路 Time Limit: 1000MS   Memory Limit: 131072KB   64bit IO Format: %lld & %llu Submit ...

  5. ytu 1050:写一个函数,使给定的一个二维数组(3×3)转置,即行列互换(水题)

    1050: 写一个函数,使给定的一个二维数组(3×3)转置,即行列互换 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 154  Solved: 112[ ...

  6. [poj2247] Humble Numbers (DP水题)

    DP 水题 Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The se ...

  7. gdutcode 1195: 相信我这是水题 GDUT中有个风云人物pigofzhou,是冰点奇迹队的主代码手,

    1195: 相信我这是水题 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 821  Solved: 219 Description GDUT中有个风云人 ...

  8. BZOJ 1303 CQOI2009 中位数图 水题

    1303: [CQOI2009]中位数图 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2340  Solved: 1464[Submit][Statu ...

  9. 第十一届“蓝狐网络杯”湖南省大学生计算机程序设计竞赛 B - 大还是小? 字符串水题

    B - 大还是小? Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format: Description 输入两个实数,判断第一个数大 ...

随机推荐

  1. zoj 2229 Ride to School

    所有车子到达的总时间算出来,然后从小到大排序,如果:1. 开始时间 < 0 的,不予考虑,太快的赶不上,太慢的赶上也没用.2. 开始时间 > 0 的,Charley 和最早到达的车子一起到 ...

  2. 浅谈Struts2(三)

    一.Struts2收集client的参数 核心思路: <form method="post" action="XXXX"> <input ty ...

  3. Python TypeError: not enough arguments for format string

    今天使用mysqldb执行query语句的时候,在执行这条语句的时候: select PROJ, DATE_FORMAT(MAX(DATE),'%Y-%m-%') AS MAXDATE, DATE_F ...

  4. BestCoder Round #46

    1001 YJC tricks time 题目链接:1001 题意:给你时针和分针所成的角度,输出现在的时间,以10秒为单位 思路:每10秒,分针走1度,时针走分针的1/12,我们可以根据时间来分别计 ...

  5. Html中截切文章内容,造成标签不全的问题

    把标签全部进行替换 ) { string strText = System.Text.RegularExpressions.Regex.Replace(html, "<[^>]+ ...

  6. git版本控制的笔记

    一.配置你的身份,提交代码时git就可以知道是谁提交的了 git config --global user.name "Tony" git config --global user ...

  7. discuz_style_default.xml修改

    首先我们需要在template/文件夹下新建一个yourstyle文件夹放置模板文件,然后复制default下的discuz_style_default.xml,重命名为discuz_style_你的 ...

  8. bootstrap读书笔记

    引入bootstrap.js或单个插件的js文件 若引入单个插件的js文件,注意插件之间的依赖关系 data属性api data属性的api很方便,但我们也可以选择关闭这个功能:$(document) ...

  9. nexus REST API /artifact/maven/[resolve|redirect] returns unexpected for v=LATEST

    Novice nexus oss (2.0.0) user here – getting unexpected results when requesting v=LATEST artifact fr ...

  10. Django里面的RequestContext

    c = RequestContext(request, { 'foo': 'bar', }) get_template('about.html').render(c) 当我们定义一个RequestCo ...