POJ 1002
2014-04-30 18:38
246 查看
思路:很坑爹的一道水题么,题目没说字符串有多长,一开始开的长度是30,一直RE。另外如果没有重复的输出的是No duplicates.,注意有句点。。。两种解法:map或者二叉搜索树中序遍历一次,维护一个cnt(记录次数)域。
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<string> #include<map> using namespace std; char str[300]; string temp; map<string, int>ss; int a[26] = {2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 9 }; void Switch(){ char s[300]; memset(s, 0, sizeof(str)); temp.clear(); int k = 0; int len = strlen(str); for(int i = 0;i < len;i ++){ if(str[i] >= '0' && str[i] <= '9') s[k++] = str[i]; if(str[i] >= 'A' && str[i] <= 'Z') s[k++] = a[str[i]-'A'] + '0'; if(k == 3) s[k++] = '-'; } temp = s; } int main(){ int n; /* freopen("in.c", "r", stdin); */ while(~scanf("%d", &n)){ ss.clear(); for(int i = 0; i < n;i ++){ memset(str, 0, sizeof(str)); scanf("%s", str); Switch(); ss[temp]++; } int flag = 0; for(map<string, int>::iterator it = ss.begin(); it != ss.end(); ++it){ if(it->second > 1){ flag = 1; cout << it->first << " " << it->second << endl; } } if(flag == 0) printf("No duplicates.\n"); } return 0; }