Etaoin Shrdlu

 

                                          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: ETAOINSHR

German: ENIRSATUD
French: EAISTNRUL
Spanish: EAOSNRILD
Italian: EAIONLRTS
Finnish: AITNESLOK
Just 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.
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;
    }
}

 

转载于:https://www.cnblogs.com/sdauyqy/p/3310881.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值