题目:http://poj.org/problem?id=3096
题意:给定一个字符串S,从中找出所有有两个字符组成的子串,每当组成子串的字符之间隔着n字符时,如果没有相同的子串出现,则输出 "S is surprising." ,
反之,则输出 "S is NOT surprising." 。 例如 AABA 把它分成两个字符为一个整体的,1..相邻两个字符 AA,AB,BA 没有相同的; 2.隔一个字符的 AB AA 没有相同; 3.隔两个字符的 AA 没有相同
map:转载自:http://blog.csdn.net/lyy289065406/article/details/6648624
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath> 6 #include <map> 7 #include <algorithm> 8 #define LL __int64 9 const int maxn = 1e3 + 100; 10 using namespace std; 11 12 int main() 13 { 14 int t, n, sum, v; 15 char s[200]; 16 while(~scanf("%d", &t)) 17 { 18 while(t--) 19 { 20 cin>>n; 21 sum = 0; 22 map<string, int>m1, m2; 23 map<string, int>::iterator it; 24 while(n--) 25 { 26 scanf("%s%d", s, &v); 27 if(m1[s]==0) m1[s] = v; 28 else if(m2[s]==0) 29 { 30 m2[s] = v; 31 if(m2[s]>m1[s]) 32 { 33 int tmp = m1[s]; 34 m1[s] = m2[s]; 35 m2[s] = tmp; 36 } 37 } 38 else 39 { 40 m2[s] = m2[s]>v?m2[s]:v; 41 if(m2[s]>m1[s]) 42 { 43 int tmp = m1[s]; 44 m1[s] = m2[s]; 45 m2[s] = tmp; 46 } 47 } 48 } 49 for(it = m1.begin(); it != m1.end(); it++) 50 sum += it->second; 51 for(it = m2.begin(); it != m2.end(); it++) 52 sum += it->second; 53 cout<<sum<<endl; 54 } 55 } 56 return 0; 57 }
1 /*STL<map>标记*/ 2 3 //Memory Time 4 //212K 16MS 5 6 #include<iostream> 7 #include<string> 8 #include<map> 9 using namespace std; 10 11 int main(void) 12 { 13 char s[80]; 14 while(cin>>s && s[0]!='*') 15 { 16 int len=strlen(s); 17 if(len<=2) //长度小于等于2的串必定是surprising String 18 { 19 cout<<s<<" is surprising."<<endl; 20 continue; 21 } 22 23 bool mark=true; //标记s是否为Surprising String 24 for(int d=0;d<=len-2;d++) //d为当前所选取的两个字母之间的距离,d(max)=len-2 25 { 26 map<string,bool>flag; 27 28 bool sign=true; //标记D-pairs字母对是不是D-unique 29 for(int i=0;i<=len-d-2;i++) //i为所选取的两个字母中第一个字母的下标 30 { 31 char pair[3]={s[i],s[i+d+1],'