zoukankan      html  css  js  c++  java
  • 杭电1002 Etaoin Shrdlu

    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.
    Input
     
    The input contains several test cases. Each starts with a number n on a separate line, denoting the number of lines of the test case. The input is terminated by n=0. Otherwise, 1<=n<=64, and there follow n lines, each with a maximal length of 80 characters. The concatenation of these n lines, where the end-of-line characters are omitted, gives the text sample you have to examine. The text sample will contain printable ASCII characters only.
     
    Output
    For each test case generate 5 lines containing the top 5 digrams together with their absolute and relative frequencies. Output the latter rounded to a precision of 6 decimal places. If two digrams should have the same frequency, sort them in (ASCII) lexicographical order. Output a blank line after each test case.
     
    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行字符串,两个字符为一组;统计他们出现的频率,输出频率的前五位;频率相同按字典序输出;
     
    AC代码:
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 
     5 using namespace std;
     6 
     7 int main()
     8 {
     9     int dp[130][130]={0};//辅助数组。
    10     char m[100];//对字符串的输入
    11     string str;//合成字符的存储;
    12     int max=0;//存储最大的次数
    13     int n;//存储行数;
    14     int j,i;//循环变量;
    15     int leng;//存储总长度;
    16     char a,b;
    17     while(cin>>n)
    18     {
    19         gets(m);//录入n后的回车;
    20         if(n==0)break;//当n等于0是跳出;
    21         memset(dp,0,sizeof(dp));
    22         gets(m);//录入第一行;
    23         str=m;//存入str中
    24         for(i=2;i<=n;i++)
    25         {
    26             gets(m);//录入第i行;
    27             str+=m;//存入str中;
    28         }
    29         leng=str.length();//读出str的长度;
    30         for(i=0;i<=leng-2;i++)//逐个检测;
    31         {
    32             dp[str[i]][str[i+1]]++;
    33         }
    34         max=0;
    35         double sum=0;
    36         for(i=0;i<128;i++)
    37         {
    38             for(j=0;j<128;j++)
    39             {
    40                 if(dp[i][j]>max)max=dp[i][j];
    41                 sum+=dp[i][j];
    42             }
    43         }
    44         n=0;
    45         while(max)
    46         {
    47             for(i=0;i<128;i++)
    48             {
    49                 for(j=0;j<128;j++)
    50                 {
    51                     if(dp[i][j]==max)
    52                     {
    53                         a=i;
    54                         b=j;
    55                         cout<<a<<b<<" "<<max<<" ";
    56                         printf("%.6f
    ",max/sum);
    57                         n++;
    58                     }
    59                     if(n==5)break;//输出5组停止
    60                 }
    61                 if(n==5)break;//输出5组停止
    62             }
    63             max--;
    64             if(max==0||n==5)break;//输出5组停止,输出不够5组停止;
    65         }
    66         cout<<endl;//每个样例输出结束后输出一行空格;
    67     }
    68     return 0;
    69 }
    View Code
     

     

  • 相关阅读:
    spring boot actuator监控需要注意的点
    spring boot actuator端点高级进阶metris指标详解、git配置详解、自定义扩展详解
    3、尚硅谷_SSM高级整合_创建Maven项目.avi
    elasticSearch插件metricbeat收集nginx的度量指标
    elasticSearch插件的安装以及使用nginx的modles收集nginx的日志
    服务治理平台微服务介绍
    skywalking面板功能介绍2
    skywalking中表字段的信息
    js总结33 :javascript-DOM节点属性
    js教程系列32 :javascript-DOM节点操作
  • 原文地址:https://www.cnblogs.com/zhangchengbing/p/3241759.html
Copyright © 2011-2022 走看看