POJ-1002
来源:http://poj.org/problem?id=1002&lang=zh-CN
这题用C++的string来写真的很蛋疼,效率低不说,输入输出真的麻烦。由此看出,C++的STL效率不行,不过除非是那种特别卡时的题,一般STL还是不错的。
这是C++的代码:
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> using namespace std; int dd[]={2,2,2,3,3,3, 4,4,4,5,5,5,6,6,6, 7,0,7,7,8,8,8,9,9,9,0}; string s[100005]; int f[100005]={0}; bool flag=true; void print(string s,int x) { s.insert(3,"-",1); //这里用insert函数效率太低,不过我懒得写循环输出 cout<<s<<" "<<x<<endl; flag=false; } int main() { char ss[20]; int n; scanf("%d\n",&n); //这里一定要加换行符\n,不加肯定错 for (int i=1;i<=n;i++) { s[i].clear(); gets(ss); for (int j=0;ss[j];j++) if (ss[j]>='A' && ss[j]<='Z') s[i]+=dd[ss[j]-'A']+'0'; else if (ss[j]!='-') s[i]+=ss[j]; } sort(s+1,s+n+1); int x=1; string temp=s[1]; for (int i=2;i<=n;i++) { if (s[i]==temp) x++; else { if (x>1) print(temp,x); x=1; temp=s[i]; } } if (s[n]==s[n-1]) print(s[n],x); if (flag) printf("No duplicates.\n"); return 0; }
下面的是我早些时候写的pascal代码,效率还可以:
program eee; const a:array['A'..'Z'] of char=('2','2','2','3','3','3','4','4','4','5','5','5', '6','6','6','7','0','7','7','8','8','8','9','9','9', '0'); var n,i,j,k,l:longint; d:array[0..100001] of string; s:string; flag:boolean; procedure sort(l,r:longint); var i,j:longint; k,x:string; begin i:=l; j:=r; k:=d[(l+r) div 2]; repeat while d[i]<k do inc(i); while d[j]>k do dec(j); if i<=j then begin x:=d[i]; d[i]:=d[j]; d[j]:=x; inc(i); dec(j); end; until i>j; if i<r then sort(i,r); if l<j then sort(l,j); end; begin readln(n); for i:=1 to n do begin readln(d[i]); s:=''; for j:=1 to length(d[i]) do begin if d[i,j] in ['A'..'Z'] then s:=s+a[d[i,j]] else if d[i,j] in ['0'..'9'] then s:=s+d[i,j]; end; insert('-',s,4); d[i]:=s; end; sort(1,n); flag:=true; s:=d[1]; k:=1; for i:=2 to n do begin if (d[i]<>s) and (k>1) then begin flag:=false; writeln(s,' ',k); s:=d[i]; k:=1; end else if d[i]<>s then s:=d[i] else inc(k); end; if d[n]=d[n-1] then begin writeln(d[n],' ',k); flag:=false; end; if flag then writeln('No duplicates.'); end.