题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5578

Friendship of Frog

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

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 −1 instead.
 
Sample Input
2
abcecba
abc
 
Sample Output
Case #1: 2
Case #2: -1
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char str[];
int main()
{
int t;
scanf("%d",&t);
int cnt=;
while(t--)
{
scanf("%s",str);
int len=strlen(str);
int ans=,flag=;
for(int i=;i<len;i++)
{
for(int j=i+;j<len;j++)
{
if(str[j]==str[i])
{
ans=min(ans,j-i);
flag=;
break;
}
}
}
if(!flag)ans=-;
printf("Case #%d: %d\n",cnt++,ans);
}
return ;
}

最新文章

  1. gitlab
  2. JS数组push会覆盖前面的数据问题
  3. ASP.NET MVC 微信公众号支付,微信公众平台配置
  4. 前端上传组件Plupload使用指南
  5. 自动化基础普及之selenium是啥?
  6. 二维背包(钟神想要的)(不是DP)
  7. 006医疗项目-模块一:用户的查找:2.用户表查询的mapper映射的文件
  8. ecshop后台admin路径怎么修改
  9. C开发基础--函数调用栈
  10. Centos升级内核 --已验证
  11. Swing 顶层容器
  12. NSCondition的用法,NSCondication实现线程同步,生产者消费问题实现(转载)
  13. datazen Active Directory AD 配置
  14. UC网盘被关停 将与阿里淘盘合并?(案例说明云盘的成本才是重点,技术不是问题;与巨头竞争是重点)
  15. mysql之 mysql 5.6不停机主主搭建(活跃双主基于日志点复制)
  16. xilinx Vivado的使用详细介绍(2):创建工程、添加文件、综合、实现、管脚约束、产生比特流文件、烧写程序、硬件验证
  17. 对css语法中position值的理解
  18. JAVAEE企业级应用开发浅谈之MVC 中的V-VIEW视图
  19. POJ1015-Jury Compromise-dp
  20. python学习小记

热门文章

  1. saltstack之文件管理
  2. Android Studio 2.0 稳定版新特性介绍
  3. Unity中surfaceShader的处理机制和finalColor
  4. PHP-Manual的学习----【语言参考】----【类型】-----【float浮点型】
  5. python爬虫学习研究
  6. 二、Android应用的界面编程(一)界面编程与视图(View)组件
  7. winphone 中的指针和手势事件
  8. 我为什么选择采用node.js来做新一代的EasyDarwin RTSP开源流媒体服务器
  9. php在不同平台下路径分隔符不同的解决办法
  10. 【译】Stackoverflow:Java Servlet 工作原理问答