这个题目题意挺简单,就是给定一个字符串,按一定规则转化为给定格式的字符串。首先把字母转数字,再把数字相加成一个7位数,根据大小排序,对相同的数字计数,再控制输出每一位即可。易错点在于:字母转数字时很容易将Q和Z略过,但是数字相加的时候就容易忘记Q和Z的存在。在寻找相邻排列的相同数字时也要注意计数的方法。
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
long a[100001];
bool com(const int &x,const int&y)
{
return x<y;
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
string s;
cin>>s;
for(int j=0;j<s.length();j++)
{
if(s[j]=='Q'||s[j]=='Z'||s[j]=='-')
{
continue;
}
if(s[j]=='A'||s[j]=='B'||s[j]=='C')
{
s[j]='2';
}
if(s[j]=='D'||s[j]=='E'||s[j]=='F')
{
s[j]='3';
}
if(s[j]=='G'||s[j]=='H'||s[j]=='I')
{
s[j]='4';
}
if(s[j]=='J'||s[j]=='K'||s[j]=='L')
{
s[j]='5';
}
if(s[j]=='M'||s[j]=='N'||s[j]=='O')
{
s[j]='6';
}
if(s[j]=='P'||s[j]=='R'||s[j]=='S')
{
s[j]='7';
}
if(s[j]=='T'||s[j]=='U'||s[j]=='V')
{
s[j]='8';
}
if(s[j]=='W'||s[j]=='X'||s[j]=='Y')
{
s[j]='9';
}
}
long k=0;
long m=1;
for(int j=s.length()-1;j>=0;j--)
{
if(s[j]=='-'||s[j]=='Q'||s[j]=='Z')
{
continue;
}
else
{
k+=((int)s[j]-(int)'0')*m;
m*=10;
}
}
a[i]=k;
}
sort(a+1,a+n+1,com);
bool check=true;
int t=0;
int count=1;
for(int i=2;i<=n+1;i++)
{
if(i<=n&&a[i]==a[i-1])
{
t=a[i];
count++;
}
else
{
if(count>=2)
{
int k=a[i-1];
int t1=k/10000;
cout<<t1/100;
cout<<(t1-t1/100*100)/10;
cout<<t1%10;
cout<<"-";
t1=k%10000;
cout<<t1/1000;
t1=t1-t1/1000*1000;
cout<<t1/100;
t1=t1-t1/100*100;
cout<<t1/10;
cout<<t1%10;
cout<<" "<<count<<endl;
check=false;
}
count=1;
}
}
if(check)
{
cout<<"No duplicates.";
}
return 0;
}