hdu 5578 Friendship of Frog(multiset的应用)

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 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.
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
 
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=0;
31    scanf("%d",&t);
32    while(t--){
33       for(int i=0;i<N;i++){
34          ms[i].clear();
35       }
36       scanf("%s",s);
37       int len=strlen(s);
38       for(int i=0;i<len;i++){
39          int u=s[i]-'a';
40          ms[u].insert(i);
41       }
42       int ans=1000000;
43       for(int i=0;i<26;i++){
44          //it=ms[i].begin();
45          int minnn=1000000;
46          int size_=ms[i].size();
47          int num=0;
48          for(it1=ms[i].begin();it1!=ms[i].end();it1++){
49             if(size_==1){
50                break;
51             }
52             if(num>=size_-1){
53                break;
54             }
55             //printf("%d= %d
",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
",minnn);
66          ans=min(ans,minnn);
67 
68       }
69       printf("Case #%d: ",++ac);
70       if(ans==1000000){
71          printf("-1
");
72       }else
73       printf("%d
",ans);
74    }
75     return 0;
76 }
View Code
原文地址:https://www.cnblogs.com/UniqueColor/p/5011296.html