UVa: 755 - 487--3279

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值