hdu 5578 Friendship of Frog(multiset的应用)
2015-12-01 21:00
471 查看
[align=left]Problem Description[/align]
[align=left]Input[/align]
[align=left]Output[/align]
[align=left]Sample Input[/align]
[align=left]Sample Output[/align]
[align=left]Source[/align]
2015ACM/ICPC亚洲区上海站-重现赛(感谢华东理工)
multiset随便搞搞就出来了。
View Code
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 the2nd 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
[align=left]Source[/align]
2015ACM/ICPC亚洲区上海站-重现赛(感谢华东理工)
multiset随便搞搞就出来了。
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<math.h> #include<algorithm> #include<queue> #include<set> #include<bitset> #include<map> #include<vector> #include<stdlib.h> #include <stack> using namespace std; #define PI acos(-1.0) #define max(a,b) (a) > (b) ? (a) : (b) #define min(a,b) (a) < (b) ? (a) : (b) #define ll long long #define eps 1e-10 #define MOD 1000000007 #define N 1006 #define inf 1e12 multiset<int>ms ; multiset<int>::iterator it,it1,it2; char s ; int main() { int t; int ac=0; scanf("%d",&t); while(t--){ for(int i=0;i<N;i++){ ms[i].clear(); } scanf("%s",s); int len=strlen(s); for(int i=0;i<len;i++){ int u=s[i]-'a'; ms[u].insert(i); } int ans=1000000; for(int i=0;i<26;i++){ //it=ms[i].begin(); int minnn=1000000; int size_=ms[i].size(); int num=0; for(it1=ms[i].begin();it1!=ms[i].end();it1++){ if(size_==1){ break; } if(num>=size_-1){ break; } //printf("%d= %d\n",i,(*it1)); num++; it2=it1; it2++; int dis=(*it2)-(*it1); if(dis<minnn){ minnn=dis; } } //printf("minnn = %d\n",minnn); ans=min(ans,minnn); } printf("Case #%d: ",++ac); if(ans==1000000){ printf("-1\n"); }else printf("%d\n",ans); } return 0; }
View Code
相关文章推荐
- SQL Server 2005 自动化删除表分区设计方案
- cordova 安装插件合集
- 积木城堡
- 积跬步-至千里系列之一:Linux系统学习的一些初级命令
- Java对象表示方式1:序列化、反序列化和transient关键字的作用
- 汇编教程之动态链接库
- Python 多线程
- shell 学习记录
- 不用Unity库,自己实现.NET轻量级依赖注入
- 第二章 Java语言基础(2)
- 虚拟上怎么挂载镜像
- Mssql常用经典SQL语句大全完整版--详解+实例
- codeforces 414A Mashmokh and Numbers 模拟题
- 在CentOS7上安装Python3.5.0,及其与旧版本Python2.7.5的共存问题笔记
- 点餐,购物车抛物线效果
- 拼接结果
- Boost源码学习三[实用工具](4)
- 数据库封装
- JSP之监听器Listener(一)————————ServletContextListener
- C++类