直接map来模拟就好了。
代码如下:
#include<string>
#include<map>
#include<cstdio>
#include<iostream>
#include<cctype>
using namespace std;
map<string,int> mp;
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
char s[30],s0[30];
scanf("%s",s);
int first=1,k=0;
for(int j=0;s[j]!='\0';j++)
{
if(isdigit(s[j]))
{
s0[k++]=s[j];
}
else if(s[j]!='-')
{
if(s[j]>='A'&&s[j]<='C')
s0[k++]='2';
else if(s[j]>='D'&&s[j]<='F')
s0[k++]='3';
else if(s[j]>='G'&&s[j]<='I')
s0[k++]='4';
else if(s[j]>='J'&&s[j]<='L')
s0[k++]='5';
else if(s[j]>='M'&&s[j]<='O')
s0[k++]='6';
else if(s[j]>='P'&&s[j]<='S')
s0[k++]='7';
else if(s[j]>='T'&&s[j]<='V')
s0[k++]='8';
else if(s[j]>='W'&&s[j]<='Y')
s0[k++]='9';
}
if(k==3)
s0[k++]='-';
}
s0[k]='\0';//cout<<s0<<endl;
string t;
t=s0;
if(mp.find(t)!=mp.end())
{
mp[t]++;
}
else mp[t]=1;
}
int flag=0;
for(map<string,int>::iterator it=mp.begin();it!=mp.end();++it)
{
if((*it).second>1)
{
cout<<(*it).first<<' '<<(*it).second<<endl;
flag=1;
}
}
if(!flag)
cout<<"No duplicates.\n";
return 0;
}
运行时间:985ms