题目链接: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 ;
}

hdu-5578 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 5578 Friendship of Frog(multiset的应用)

    Problem Description N frogs . Two frogs are friends if they come from the same country. The closest ...

  3. hdu 5578 Friendship of Frog

    题意:给定一行字符串(都是小写字母),每一个字符都代表一只青蛙以及其国籍,若字符串中出现两个字符相同,则这两个字符所代表的青蛙来自同一国度,可称之为好朋友. 现在需要找到距离最近的好朋友并输出他们的距 ...

  4. Friendship of Frog(水题)

    Friendship of Frog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  5. HDU 2920 分块底数优化 暴力

    其实和昨天写的那道水题是一样的,注意爆LL $1<=n,k<=1e9$,$\sum\limits_{i=1}^{n}(k \mod i) = nk - \sum\limits_{i=1}^ ...

  6. 【HDU 5578】Friendship of Frog

    题 题意 求相同字母最近距离 分析 用数组保存各个字母最后出现的位置,维护最小距离. 代码 #include <cstdio> int c[30],n,p,a,minl; char ch; ...

  7. hdu 5277 YJC counts stars 暴力

    YJC counts stars Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...

  8. HDU 5762 Teacher Bo (暴力)

    Teacher Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5762 Description Teacher BoBo is a geogra ...

  9. hdu 4712 Hamming Distance(随机函数暴力)

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...

随机推荐

  1. Hadoop环境搭建1_JDK+SSH

    1 前言: Hadoop 最早是为了在Linux 平台上使用而开发的,但是Hadoop 在UNIX.Windows 和Mac OS X 系统上也运行良好.不过,在Windows 上运行Hadoop 稍 ...

  2. Nearest Common Ancestors(LCA)

    Description A rooted tree is a well-known data structure in computer science and engineering. An exa ...

  3. 怎样过滤跨站恶意脚本攻击(XSS)

    什么是XSS? XSS(Cross Site Scripting),即跨站脚本攻击,是一种常见于web application中的计算机安全漏洞.XSS通过在用户端注入恶意的可运行脚本,若服务器端对用 ...

  4. debian安装oracle jdk

    1 去官网下载linux jdk https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.htm ...

  5. iOS UICollection 和UITableview新特性

    很详细优秀的博客: http://www.jianshu.com/p/e97780a24224 iOS10新特性总结 http://blog.csdn.net/yyacheng/article/det ...

  6. thinkphp将APP_DEBUG常量设置为false后报错的问题

    ThinkPHP 将 APP_DEBUG 常量设置为 false 后出现了下面的问题: Parse error: syntax error, unexpected T_STRING in \www\R ...

  7. python初学者总结

    学习python首先配置好工作环境,因为不同版本之间的python是不兼容了 原创:01coding.com win7安装环境过程: 1:下载python 建议下载两个不同版本官方已给出 https: ...

  8. mysql 自连接查询数据

    今天项目BOSS提了个sql语句需求,我听得稀里糊涂,没办法,一步步讨论.实践,最终搞定. 先上建表语句: /* Navicat MySQL Data Transfer Source Server : ...

  9. vuex源码 安装依赖问题

    今天下载vuex源码时 安装依赖出现以下问题 > chromedriver@2.32.3 install /Users/bao/Desktop/vue-store/vuex/node_modul ...

  10. sqlalchemy——基本操作

    以下所有代码片段都使用了统一的引用,该引用如下: from sqlalchemy import create_engine, ForeignKey from sqlalchemy.ext.declar ...