hdu 5578 Friendship of Frog
2016-07-17 13:06
239 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5578
题目:
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2
abcecba
abc
[align=left]Sample Output[/align]
Case #1: 2
Case #2: -1
map乱搞一下就好。
#include <iostream>
#include<bits/stdc++.h>
#define INF 0x7fffffff
using namespace std;
char s[1100];
int main()
{
int T;
cin>>T;
for(int k=1;k<=T;k++)
{
scanf("%s",s);
int n=strlen(s);
map<char,int>mp;
int ans=INF;
for(int i=0;i<n;i++)
{
if(mp[s[i]])
ans=min(i+1-mp[s[i]],ans);
mp[s[i]]=i+1;
}
printf("Case #%d: %d\n",k,ans==INF?-1:ans);
}
}
题目:
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2
abcecba
abc
[align=left]Sample Output[/align]
Case #1: 2
Case #2: -1
map乱搞一下就好。
#include <iostream>
#include<bits/stdc++.h>
#define INF 0x7fffffff
using namespace std;
char s[1100];
int main()
{
int T;
cin>>T;
for(int k=1;k<=T;k++)
{
scanf("%s",s);
int n=strlen(s);
map<char,int>mp;
int ans=INF;
for(int i=0;i<n;i++)
{
if(mp[s[i]])
ans=min(i+1-mp[s[i]],ans);
mp[s[i]]=i+1;
}
printf("Case #%d: %d\n",k,ans==INF?-1:ans);
}
}
相关文章推荐
- Android学习过程中遇到的一些名词
- NOIP 2014 T2 联合权值 DFS
- 求最大数
- Java ConcurrentModificationException异常原因和解决方法
- 二分图匹配(匈牙利算法)
- Ubuntu下查看软件版本及安装位置
- 旧版滨州学院CSDN高校俱乐部博客
- d3图表中饼图的使用示例
- 单词翻译
- 安卓项目之淘忆的功能需求分析
- linux 网络操作命令
- CSDN博客如何转载(技术贴)
- android环境搭建
- Spark入门之WordCount
- java 进阶编程练习(字符串)
- FormatMessage 格式化 GetLastError 错误代码
- NGUI学习笔记1-菜单
- 【ajax系列】Spring4 mvc配置JSON返回
- 编写一个基于JQuery的自定义导航小框架
- 事件