题目链接:http://poj.org/problem?id=1002
解题思路:
此题有坑,刚开始输入字符串是一定要用char *,用string的话会超时。
思路很简单,就是边输入边处理,将字符串处理成要求格式。这里用到map大法,各种hash映射。最后记得要排下序(升序),如果没有满足条件的话,要输出No duplicates.
完整代码:
//#include <bits/stdc++.h>
#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <climits>
#include <cassert>
#include <complex>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long LL;
typedef double DB;
typedef unsigned uint;
typedef unsigned long long uLL;
/*
__gcd(a, b) // My gcd
__builtin_ffs(x) //My lowbit
__builtin_popcount(x) //The number of 1-bits
*/
/** Constant List .. **/ //{
const int MOD = int(1e9)+7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const DB EPS = 1e-9;
const DB OO = 1e20;
const DB PI = acos(-1.0); //M_PI;
const int maxn = 100001;
char a[maxn];
map<char , char> m;
map<int , string> s;
map<string , int>vis;
string result[maxn];
int main()
{
#ifdef DoubleQ
freopen("in.txt","r",stdin);
#endif
std::ios::sync_with_stdio(false);
std::cin.tie(0);
m['A'] = m['B'] = m['C'] = '2';
m['D'] = m['E'] = m['F'] = '3';
m['G'] = m['H'] = m['I'] = '4';
m['J'] = m['K'] = m['L'] = '5';
m['M'] = m['N'] = m['O'] = '6';
m['P'] = m['R'] = m['S'] = '7';
m['T'] = m['U'] = m['V'] = '8';
m['W'] = m['X'] = m['Y'] = '9';
int n;
while(cin >> n)
{
s.clear();
vis.clear();
int cnt = 0;
for(int i = 0 ; i < n ; i ++)
{
cin >> a;
//cin >> a[i];
string t = "";
for(int j = 0 ; a[j] ; j ++)
{
if(a[j] == '-')
continue;
else if(a[j] >= '0' && a[j] <= '9')
t += a[j];
else
t += m[a[j]];
}
if(vis[t] == 0)
{
s[cnt ++] = t;
vis[t] ++;
}
else
vis[t] ++;
}
int tt = 0;
for(int i = 0 ; i < cnt ; i ++)
{
if(vis[s[i]] > 1)
{
result[tt ++] = s[i];
}
}
if(tt == 0)
{
printf("No duplicates.\n");
continue;
}
sort(result , result + tt);
for(int i = 0 ; i < tt ; i ++)
{
for(int j = 0 ; j <=2 ; j ++)
cout << result[i][j];
cout << "-";
for(int j = 3 ; j < 7 ; j ++)
cout << result[i][j];
cout << " " << vis[result[i]] << endl;
}
}
}