POJ 1002
自己写了个程序,测试用例过了,但是出现提交上去说是Runtime Error!!
题目地址http://poj.org/problem?id=1002
#include <iostream>#include<cstring>using namespace std;const char map[] = {2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 0, 7, 7, 8, 8, 8, 9, 9, 9};//typedef char (*Tel)[16];char telephone[1000000][16];char result[1000000][9];int Deal(int count[], int nCases);void Output(int count[], int nCount);int main(){ int nCases; int index = 0; cin >> nCases; int *count = new int[nCases]; char temp[16]; while (index < nCases) { cin >> temp; strcpy(telephone[index++], temp); } int nCount = Deal(count, nCases); bool out = false; for (int i = 0; i < nCount; i++) if (count[i] > 1) { out = true; break; } if (!out) cout << "No duplicates" << endl; else Output(count, nCount); delete []count; return 0;}int Deal(int count[], int nCases){ char temp[9]; int nCount = 0; temp[8] = '\0'; for (int i = 0; i < nCases; i++) count[i] = 0; for (int i = 0; i < nCases; i++) { int len = strlen(telephone[i]); int k = 0; int j; for (j = 0; j < len; j++) { if (telephone[i][j] >= 'A' && telephone[i][j] <= 'Z') temp[k++] = map[telephone[i][j]-'A'] + '0'; else if (telephone[i][j] >= '0' && telephone[i][j] <= '9') temp[k++] = telephone[i][j]; if (k == 3) temp[k++] = '-'; } int rCmp; for (j = 0; j < nCount; j++) if ((rCmp = strcmp(temp, result[j])) <= 0) break; if (rCmp == 0) count[j]++; else { nCount++; for (k = nCount-1; k > j; k--) { strcpy(result[k], result[k-1]); count[k] = count[k-1]; } strcpy(result[j], temp); count[j]=1; } } return nCount;}void Output(int count[], int nCount){ for (int i = 0; i < nCount; i++) if (count[i] != 1) cout << result[i] << " " << count[i] << endl;}#include<iostream>#include<algorithm>#include<fstream>#include<vector>#include<string>using namespace std;int arr[]={2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,0,7,7,8,8,8,9,9,9};int main(){ int n; //ifstream cin("in.txt"); while(cin>>n){ string str; vector<string> vc; while(n--){ cin>>str; string temp; for(int i=0;i!=str.size();++i){ if(str[i]>='0'&&str[i]<='9') temp.push_back(str[i]); else if(str[i]>='A'&&str[i]<='Z') temp.push_back(arr[str[i]-'A']+'0'); } vc.push_back(temp); } sort(vc.begin(),vc.end()); int ans=1; bool flag; string st; for(int i=0;i!=vc.size()-1;++i){ if(vc[i+1]==vc[i]){ st=vc[i]; ++ans; } else{ if(ans>1){ flag=true; cout<<st[0]<<st[1]<<st[2]<<"-"<<st[3]<<st[4]<<st[5]<<st[6]<<" "<<ans<<endl; } ans=1; } } if(ans>1) cout<<st[0]<<st[1]<<st[2]<<"-"<<st[3]<<st[4]<<st[5]<<st[6]<<" "<<ans<<endl; else if(!flag) cout<<"No duplicates."<<endl; } return 0;}
[解决办法]
测试数据里有陷阱所以得注意输入输出的格式。