您的位置:首页 > 其它

poj1002 487-3279(哈希)

2014-08-27 13:58 274 查看
hash+sort

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

int num
;

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
;

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: