Problem Description
  1. 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 N1th and the Nth frog, etc) are exactly . Two frogs are friends if they come from the same country.
  2.  
  3. The closest friends are a pair of friends with the minimum distance. Help us find that distance.
Input
  1. First line contains an integer T, which indicates the number of test cases.
  2.  
  3. Every test case only contains a string with length N, and the ith character of the string indicates the country of ith frogs.
  4.  
  5. T≤.
  6.  
  7. for % data, N≤.
  8.  
  9. for % data, N≤.
  10.  
  11. the string only contains lowercase letters.
 
Output
  1. For every test case, you should output "Case #x: y", where x indicates the case number and counts from and y is the result. If there are no frogs in same country, output instead.
 
Sample Input
  1. abcecba
  2. abc
 
Sample Output
  1. Case #:
  2. Case #: -
 
Source

multiset随便搞搞就出来了。

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<cmath>
  6. #include<math.h>
  7. #include<algorithm>
  8. #include<queue>
  9. #include<set>
  10. #include<bitset>
  11. #include<map>
  12. #include<vector>
  13. #include<stdlib.h>
  14. #include <stack>
  15. using namespace std;
  16. #define PI acos(-1.0)
  17. #define max(a,b) (a) > (b) ? (a) : (b)
  18. #define min(a,b) (a) < (b) ? (a) : (b)
  19. #define ll long long
  20. #define eps 1e-10
  21. #define MOD 1000000007
  22. #define N 1006
  23. #define inf 1e12
  24. multiset<int>ms[N];
  25. multiset<int>::iterator it,it1,it2;
  26. char s[N];
  27. int main()
  28. {
  29. int t;
  30. int ac=;
  31. scanf("%d",&t);
  32. while(t--){
  33. for(int i=;i<N;i++){
  34. ms[i].clear();
  35. }
  36. scanf("%s",s);
  37. int len=strlen(s);
  38. for(int i=;i<len;i++){
  39. int u=s[i]-'a';
  40. ms[u].insert(i);
  41. }
  42. int ans=;
  43. for(int i=;i<;i++){
  44. //it=ms[i].begin();
  45. int minnn=;
  46. int size_=ms[i].size();
  47. int num=;
  48. for(it1=ms[i].begin();it1!=ms[i].end();it1++){
  49. if(size_==){
  50. break;
  51. }
  52. if(num>=size_-){
  53. break;
  54. }
  55. //printf("%d= %d\n",i,(*it1));
  56. num++;
  57. it2=it1;
  58.  
  59. it2++;
  60. int dis=(*it2)-(*it1);
  61. if(dis<minnn){
  62. minnn=dis;
  63. }
  64. }
  65. //printf("minnn = %d\n",minnn);
  66. ans=min(ans,minnn);
  67.  
  68. }
  69. printf("Case #%d: ",++ac);
  70. if(ans==){
  71. printf("-1\n");
  72. }else
  73. printf("%d\n",ans);
  74. }
  75. return ;
  76. }

hdu 5578 Friendship of Frog(multiset的应用)的更多相关文章

  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

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

  3. hdu-5578 Friendship of Frog(暴力)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5578 Friendship of Frog Time Limit: 2000/1000 MS (Jav ...

  4. Friendship of Frog(水题)

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

  5. 【HDU 5578】Friendship of Frog

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

  6. 2017多校第10场 HDU 6180 Schedule 贪心,multiset

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6180 题意:给了一些任务的开始时间和终止时间,现在让我们安排k台及机器,让这些任务在k太机器上最小,并 ...

  7. HDU 4302 Holedox Eating(multiset)

    http://acm.hdu.edu.cn/showproblem.php?pid=4302 题意: 在一条直线上,会有多条命令,如果是0,那么就会在x位置处出现一个蛋糕,如果是1,某人就会找到最近的 ...

  8. HDU 4022 Bombing (map + multiset)

    题意: 在x,y坐标范围为10 ^ -9 ~~ 10 ^ 9的坐标轴之中,有 10W个点(注意有些点可能在同一坐标上),然后有10W个询问,处理询问按照输入顺序处理,对于每个询问a,b    a == ...

  9. hdu 4268 Alice and Bob(multiset|段树)

    Alice and Bob Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

随机推荐

  1. android TextView EditTextView一些技巧使用 (视图代码布局)

    android TextView 是最常用的控件 可以用作普通的显示,还可以用作有显示文字的按钮,用作有显示图片的图文组合 1. 图文组合 xml 中: <TextView android:id ...

  2. 【配置】如何配置Tp-link无线路由器作为无线交换机

    最近刚装了电信天翼宽带,谁知道现在只能装光猫.你说一个4M带宽有必要用光猫么?装好之后问题来了,这个光猫很奇葩,只有一个以太网接口,确有内置有路由功能,也就是只有一个出口的路由器.现在问题来了:我想接 ...

  3. js设计模式

    http://www.csdn.net/article/2011-09-02/303983 阐明JavaScript设计模式.CSDN研发频道对此文进行了整理选取部分内容,供开发者学习.参考. 内容如 ...

  4. bash 学习笔记(一)

    尽量使用printf而不要用echo(echo再不同情况下语义不同) 整数%d,小数后6位%f,科学计数法 %e,16进制 %x 宽度限制 %8s %-15s 正数朝右对齐 负数朝左对齐:%04d\n ...

  5. Number Sequence(kmp)

        Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  6. PHP-xml-1

    SimpleXML 解析xml 假设xml没有声明encoding默认使用utf-8进行解码.终于得到的数据编码为utf-8格式. 我猜想它内部实现了iconv(encoding='utf-8', ' ...

  7. PHP高级特性基础

    php对象在内存中的区域:php对象名和对象存放的位置是不一样的,这一点和java是一模一样的,所以在php面向对象部分你完全可以套用java的思想去做.对象存放在堆区(heap)而对象名则和其他普通 ...

  8. 月赛-Crackhash

    Crackhash 这个题目是我为月赛出的,完全仿照自mma 1st simple_hash. 这道题目比较有意思的地方在于在32位的程序中模拟了64位的算术运算. 题目的思路很清晰.要求输入全为数字 ...

  9. plaidctf2015 uncorrupt png

    代码的执行时间挺长的,好囧! 参考了https://13c5.wordpress.com/2015/04/20/plaidctf-2015-png-uncorrupt/的代码 通过这个题目,也对Png ...

  10. Open Replicator

    Open Replicator ( http://code.google.com/p/open-replicator/ ) 开源了.Open Replicator是一个用Java编写的MySQL bi ...