1,排序。
2,计数。比较相邻str,搜索会TLE。
3,注意最后一个str的比较,我这里多加了一个"zzzzzzz"的str。
4,如果没有重复电话号码的情况应该输出"No duplicates."。
5,格式,每组数据之间有换行,最后一组数据后没有。
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
using namespace std;
char letter[30]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int len_letter=strlen(letter);
char digit[30]="2223334445556667N77888999N";
char phone[100010][20];
char ch(char c)//给大写的字母换值
{
int i;
for(i=0;i<len_letter;i++)
{
if(c==letter[i])
{c=digit[i];return c;}
}
}
void getnum(int num)//把数字字母留下,其他字符串去除。
{
int i,j;
char str[20];
for(i=0; i<num; i++)
{
int len_str=0,len=strlen(phone[i]);
for(j=0; j<len; j++)
if((phone[i][j]<='Z'&&phone[i][j]>='A')||isdigit(phone[i][j]))
str[len_str++]=phone[i][j];
str[len_str]='\0';
for(j=0; j<len_str; j++)
phone[i][j]=str[j];
phone[i][len_str]='\0';
}
}
int cmp(const void *a,const void *b)
{
return strcmp((char *)a,(char *)b);
}
int main()
{
int t;
cin>>t;
int n;
while(t--)
{
cin>>n;
getchar();
int i,j,len;
for(i=0; i<n; i++)
{
gets(phone[i]);
int num_phone=0;
len=strlen(phone[i]);
for(j=0;j<len;j++)
{
if(isdigit(phone[i][j]))
phone[i][num_phone++]=phone[i][j];
else if(isupper(phone[i][j]))
phone[i][num_phone++]=ch(phone[i][j]);
}
phone[i][num_phone]='\0';
}
for(i=0; i<7; i++)
phone[n][i]='z';
phone[n][i+1]='/0';
getnum(n);
qsort(phone,n,sizeof(phone[0]),cmp);//排序
int sum=1,count=-1;
for(i=0;i<=n;i++)//计数
{
if(strcmp(phone[i],phone[i+1])==0)
sum++;
else if(strcmp(phone[i],phone[i+1])!=0&&sum>1)
{
len=strlen(phone[i]);
for(j=0;j<len;j++)
{cout<<phone[i][j];if(j==2)cout<<"-";}
cout<<" "<<sum<<endl;
sum=1;
count=1;
}
else
sum=1;
}
if(count==-1)
cout<<"No duplicates."<<endl;
if(t)
cout<<endl;
}
return 0;
}