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. C#中两个整数相除得到带小数点的结果

    有时候需要将两个整数相除,获得带小数点的float类型数.例如一个整数12345,需要变成123.45.常见与串口与硬件通讯,DSP处理浮点型比较麻烦,DSP传递来的温度等数据都以整型的方式传递,串口 ...

  2. 捉Bug:易车注册页布局小臭虫 附demo

    --------<!--文章开始-->-------- 开场show:这几天天气异常燥热,早上起来从棉毛衣换到了小短袖,配上一杯冷泡乌龙茶,真是没谁了,这哥们懂得享受哈 开始鼓捣博客,把博 ...

  3. Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (一)

    作为一个完成的应用程序,数据存储操作是必不可少的.因此,Android系统一共提供了四种数据存储方式.分别是:SharePreference.SQLite.Content Provider和File. ...

  4. Objective-c 字典对象

    oc 中的 NSDictionary 的作用同 java 中的字典类相同,提供了 “键-值”对的组合.比如,是用字典类实现对学生姓名和学号的存放,编号是一个键(唯一性),姓名是值.它的方法有: 下面通 ...

  5. HOOK API(三)—— HOOK 所有程序的 MessageBox

    HOOK API(三) —— HOOK 所有程序的 MessageBox 0x00 前言 本实例要实现HOOK MessageBox,包括MessageBoxA和MessageBoxW,其实现细节与H ...

  6. C#操作Office.word(一)

    该文章主要是讲述如何使用VS2010创建word文档,因为在项目中我们可能需要点击一个按钮把数据库中的项目表单或图片显示到word文档中,因此该文章主要分析如何使用VS2010创建word文档并填写相 ...

  7. HDU 3729 二分匹配 反向匹配

    题意: 给定 n个学生 说的 自己 考试排名的 可能范围 确定最多几个人说真话 如果有多种答案,输出字典序最大的那种( 要求字典序最大,所以solve中从最大字典序开始匹配) 思路: 题目给定  点 ...

  8. dump文件定位程序崩溃代码行

    1.dump文件 2.程序对应的pdb 步骤一:安装windbg 步骤二:通过windbg打开crash dump文件 步骤三:设置pdb文件路径,即符号表路径 步骤四:运行命令!analyze -v ...

  9. 在OSX狮子(Lion)上安装MYSQL(Install MySQL on Mac OSX)

    这篇文章简述了在Mac OSX狮子(Lion)上安装MySQL Community Server最新版本v10.6.7的过程. MySQL是最流行的开源数据库管理系统.首先,从MySQL的下载页面上下 ...

  10. vs2013 linq to mysql

    安装文件 1.下载安装文件 2.创建项目 3.创建mysql数据连接 4.新建ADO.NET数据模型 项目实现代码 using (WorkerContainer db = new WorkerCont ...