poj1002 487-3279(哈希)
2014-08-27 13:58
274 查看
hash+sort
hash
#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; }
相关文章推荐
- poj_1002 487-3279 快速排序
- [POJ_1002]487-3279
- POJ 1002 487-3279 MAP||Sort
- poj 1002 487-3279
- poj 1002(487-3279)STL中的map
- POJ 1002 487-3279
- poj1002 487-3279
- uva 755 poj 1002 487-3279
- POJ - 1002 487-3279
- POJ - 1002 487-3279 (Trie树)
- poj 1002 487-3279
- POJ - 1002 487-3279
- POJ 1002 487-3279(排序or不排序)
- POJ1002 487-3279
- C -- POJ 1002 487-3279
- poj 1002 487-3279
- POJ 1002 487-3279
- POJ 1002 487-3279
- POJ1002 487-3279
- POJ 1002 解题报告 487-3279