思考:用G++ 超時,用C++ 500多ms.
#include#include #include #include #include #include #include using namespace std; const int maxn = 100010; int v[30] = {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 }; vector res; int cnt[maxn+5]; int main() { int n; scanf("%d", &n); getchar(); char tmp[100]; char deal[100]; res.clear(); for(int t = 1; t <= n; t++) { gets(tmp); int index = 0; int len = strlen(tmp); for(int i = 0; i < len; i++) { if(tmp[i] >= '0' && tmp[i] <= '9') { deal[index++] = tmp[i]; } else if(tmp[i] >= 'A' && tmp[i] < 'Z' && tmp[i] != 'Q') { char mid = v[tmp[i]-'A'] + '0'; deal[index++] = mid; } if(index==3) deal[index++] = '-'; } deal[index] = '\0'; res.push_back(deal); } sort(res.begin(), res.end()); memset(cnt, 0, sizeof(cnt)); cnt[0] = 1; for(int i = 1; i < (int)res.size(); i++) { if(!res[i].compare(res[i-1])) cnt[i] = cnt[i-1]+1; else cnt[i] = 1; } bool ok = false; char str[100]; int len = res.size(); for(int i = 1; i < len; i++) { if((cnt[i]>=2 && cnt[i+1]==1)||(i == len-1 && cnt[i]>=2)) { int Size = res[i].length(); res[i].copy(str, Size, 0); str[Size] = '\0'; printf("%s %d\n", str, cnt[i]); if(!ok) ok = true; } } if(!ok) printf("No duplicates. \n"); return 0; }