Etaoin Shrdlu
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Problem Description
The relative frequency of characters in natural language texts is very important for cryptography. However, the statistics vary for different languages. Here are the top 9 characters sorted by their relative frequencies for several common languages:
English: ETAOINSHRJust as important as the relative frequencies of single characters are those of pairs of characters, so called digrams. Given several text samples, calculate the digrams with the top relative frequencies.
German: ENIRSATUD
French: EAISTNRUL
Spanish: EAOSNRILD
Italian: EAIONLRTS
Finnish: AITNESLOK
Sample Input
2
Take a look at this!!
!!siht ta kool a ekaT
5
P=NP
Authors: A. Cookie, N. D. Fortune, L. Shalom
Abstract: We give a PTAS algorithm for MaxSAT and apply the PCP-Theorem [3]
Let F be a set of clauses. The following PTAS algorithm gives an optimal
assignment for F:
0
Sample Output
a 3 0.073171
!! 3 0.073171
a 3 0.073171
t 2 0.048780
oo 2 0.048780
a 8 0.037209
or 7 0.032558
. 5 0.023256
e 5 0.023256
al 4 0.018605
题意:给出n个字符串,然后把各个字符串按顺序连接起来,求出出现次数最多的前五个两个字符的组合。次数相同的按照ASCII的顺序输出
思路:这里用到了string类的 输入,加法,还有map的运用,代码很好理解。这里C++与C语言的输入输出一起使用看起来很简单,但还是少这样用为好。
代码1:(在poj 上过了,在杭电上没过,代码2两个都过了)
#include<iostream> #include<stdio.h> #include<string> #include<map> using namespace std; int main() { int l; while(cin>>l&&l) { getchar(); string str; while(l--) { string a; getline(cin,a); str+=a; } map<string, int> m; for (int i=0;i<str.length()-1;i++) { string tmp; tmp+=str[i]; tmp+=str[i+1]; m[tmp]++; } map<string,int>::iterator it, maxi; for (int i=0;i<5;i++) { int max=0; for (it=m.begin();it!=m.end();it++) { if(it->second>max) { max=it->second; maxi=it; } } printf("%s %d %.6lf\n", maxi->first.data(),max,max*1.0/(str.length() - 1)); m.erase(maxi); } cout<<endl; } return 0; }
代码2:
#include<iostream> #include<string> #include<stdio.h> using namespace std; string str[81]; string s; int main() { int n,i,j,p,t,q,max; while(cin>>n&&n!=0) { int c[128][128]={0}; s=""; cin.ignore(); for(i=0;i<n;i++) { getline(cin,str[i]); s+=str[i]; } for(i=0;i<s.length()-1;i++) { c[s[i]][s[i+1]]+=1; } for(t=1;t<=5;t++) { max=0; for(i=0;i<128;i++) for(j=0;j<128;j++) { if(c[i][j]>max) { max=c[i][j]; p=i; q=j; } else if(c[i][j]==max) { if(i<p || i==p&&j<q) { p=i; q=j; } } } printf("%c%c %d %.6f\n",(char)p,(char)q,c[p][q],c[p][q]*1.0/(s.length()-1)); c[p][q]=0; } cout<<endl; } }