Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 219174 | Accepted: 38204 |
Description
Input
Output
Sample Input
12 4873279 ITS-EASY 888-4567 3-10-10-10 888-GLOP TUT-GLOP 967-11-11 310-GINO F101010 888-1200 -4-8-7-3-2-7-9- 487-3279
Sample Output
310-1010 2 487-3279 4 888-4567 3
这个题目实现不难,但是达到相应的效率确实不容易,尝试了好几次都是超时,现直接贴代码吧!
#include <iostream> #include <string> #include <algorithm> #include <vector> #include <fstream> using namespace std; int main() { int n,i,j; char buf[150]; char fun[100]; fun['1']='1'; fun['0']='0'; fun['A']=fun['B']=fun['C']=fun['2']='2'; fun['D']=fun['E']=fun['F']=fun['3']='3'; fun['G']=fun['H']=fun['I']=fun['4']='4'; fun['J']=fun['K']=fun['L']=fun['5']='5'; fun['M']=fun['N']=fun['O']=fun['6']='6'; fun['P']=fun['R']=fun['S']=fun['7']='7'; fun['T']=fun['U']=fun['V']=fun['8']='8'; fun['W']=fun['X']=fun['Y']=fun['9']='9'; cin>>n; vector<string> v; while (n--) //ifstream fin("e:\\data.txt"); //while(fin>>buf) { cin>>buf; int c=0; string s; for (i=0;c<7;i++) { if (buf[i]=='-') continue; s+=fun[buf[i]]; c++; if (c==3) s+="-"; } v.push_back(s); } sort(v.begin(),v.end()); string temp=v[0]; int count=1; bool flag=false; for (i=1;i<v.size();i++) { if (temp==v[i]) { count++; flag=true; if (i==v.size()-1) { cout<<temp<<" "<<count<<endl; } } else { if (count>1) { cout<<temp<<" "<<count<<endl; } temp=v[i]; count=1; } } if (!flag) { cout<<"No duplicates."<<endl; } return 0; }
运行需要的内存和时间分别为:5320K ,1079MS
在讨论组里面看到一个大神的代码,太精简,效率还高,膜拜一下:
#include <cstdio> #include <algorithm> using namespace std; char s[31]; int Hash() { int sum=0; for(int i=0,k=0;k<7;i++) { if(s[i]>='0'&&s[i]<='9') { sum*=10;k++; sum+=(s[i]-'0'); } else if(s[i]>='A'&&s[i]<'Z') { sum*=10;k++; sum+=((s[i]-'A'-(s[i]>'Q'))/3+2); } } return sum; } int main() { int n;scanf("%d",&n); int data[n];getchar(); for(int tmp=0;tmp<n;tmp++) { gets(s); data[tmp]=Hash(); } sort(data,data+n); bool p=false;n--; for(int i=0,num=1;i<n;i+=num=1) { while(data[i]==data[i+1]) { num++; i++; } if(num>1) { printf("%03d-%04d %d\n",data[i]/10000,data[i]%10000,num); p=true; } } if(!p)printf("No duplicates.\n"); return 0; }