poj1002 487-3279(哈希)

hash+sort

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 100005
using namespace std;

int num[N];

int exc(char a)
{
    if(a=='A'||a=='B'||a=='C') return 2;
    if(a=='D'||a=='E'||a=='F') return 3;
    if(a=='G'||a=='H'||a=='I') return 4;
    if(a=='J'||a=='K'||a=='L') return 5;
    if(a=='M'||a=='N'||a=='O') return 6;
    if(a=='P'||a=='R'||a=='S') return 7;
    if(a=='T'||a=='U'||a=='V') return 8;
    if(a=='W'||a=='X'||a=='Y') return 9;
}

int main()
{
    int n;
    char s[50];
    //freopen("d:\\test.txt","r",stdin);
    scanf("%d",&n);
    memset(num,0,sizeof(num));
    for(int i=0; i<n; i++)
    {
        int x=0;    //save telephone as number
        scanf("%s",s);
        for(int j=0; s[j]!='\0'; j++)
        {
            if(s[j]=='-'||s[j]=='Q'||s[j]=='Z') continue;
            else if(s[j]<='9')
            {
                x=x*10+s[j]-'0';
            }
            else if(s[j]<'Z')
            {
                x=x*10+exc(s[j]);
            }
        }
        num[i]=x;
    }
    sort(num,num+n);
    bool flag=true;
    for(int i=0; i<n;)
    {
        int time=0;
        int k=num[i];
        bool ok=false;
        while(k==num[i]&&i<n)
        {
            time++;
            i++;
            if(time==2)
            {
                flag=false;
                ok=true;
            }
        }
        if(ok)
        {
            printf("%03d-%04d %d\n",k/10000,k%10000,time);
        }
    }
    if(flag) cout<<"No duplicates."<<endl;
    return 0;
}

hash

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iomanip>
#define N 10000000
using namespace std;

int time[N];

int exc(char a)
{
    if(a=='A'||a=='B'||a=='C') return 2;
    if(a=='D'||a=='E'||a=='F') return 3;
    if(a=='G'||a=='H'||a=='I') return 4;
    if(a=='J'||a=='K'||a=='L') return 5;
    if(a=='M'||a=='N'||a=='O') return 6;
    if(a=='P'||a=='R'||a=='S') return 7;
    if(a=='T'||a=='U'||a=='V') return 8;
    if(a=='W'||a=='X'||a=='Y') return 9;
}

int main()
{
    int n;
    char s[50];
    //freopen("d:\\test.txt","r",stdin);
    scanf("%d",&n);
    memset(time,0,sizeof(time));
    bool flag=false;
    for(int i=0; i<n; i++)
    {
        int x=0;    //save telephone as number
        scanf("%s",s);
        for(int j=0; s[j]!='\0'; j++)
        {
            if(s[j]=='-'||s[j]=='Q'||s[j]=='Z') continue;
            else if(s[j]<='9')
            {
                x=x*10+s[j]-'0';
            }
            else if(s[j]<'Z')
            {
                x=x*10+exc(s[j]);
            }
        }
        time[x]++;
        if(time[x]>1) flag=true;
    }
    for(int i=0;i<N;i++)
    {
        if(time[i]>1)
        {
            printf("%03d-%04d %d\n",i/10000,i%10000,time[i]);
        }
    }
    if(!flag) cout<<"No duplicates."<<endl;
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值