People often have a preference among synonyms of the same word. For example, some may prefer "the police", while others may prefer "the cops". Analyzing such patterns can help to narrow down a speaker's identity, which is useful when validating, for example, whether it's still the same person behind an online avatar.

Now given a paragraph of text sampled from someone's speech, can you find the person's most commonly used word?

Input Specification:

Each input file contains one test case. For each case, there is one line of text no more than 1048576 characters in length, terminated by a carriage return '\n'. The input contains at least one alphanumerical character, i.e., one character from the set [0-9 A-Z a-z].

Output Specification:

For each test case, print in one line the most commonly occurring word in the input text, followed by a space and the number of times it has occurred in the input. If there are more than one such words, print the lexicographically smallest one. The word should be printed in all lower case. Here a "word" is defined as a continuous sequence of alphanumerical characters separated by non-alphanumerical characters or the line beginning/end.

Note that words are case insensitive.

Sample Input:

Can1: "Can a can can a can?  It can!"

Sample Output:

can 5

例子是个坑,一开始认为 格式是can1:”_______”   ______为要检查的内容。

其实“ne character from the set [0-9 A-Z a-z]”,所以“can1”是一种字符,有别于“can”

还有就是容易超时,后来想了一个O(n)的算法,如下:

 #include <iostream>

 #include <map>

 #include <string>

 using namespace std;

 struct word

 {

    int time;

    int len;

 };

  map<string,word>  mm;

 void fun1(string ss)

 {

    int i=;string tem="";

    while(i<ss.length())

    {

         while(i<ss.length())

     {

       if((ss[i]>='A'&&ss[i]<='Z')||(ss[i]>='a'&&ss[i]<='z')||(ss[i]>=''&&ss[i]<=''))

         {

               if(ss[i]>='A'&&ss[i]<='Z')

                     ss[i]=ss[i]-'A'+'a';

               tem+=ss[i];

               ++i;

         }

         else break;

     }

      ++mm[tem].time;

       mm[tem].len=i;

       tem="";

       while(i<ss.length())

      {

       if((ss[i]>='A'&&ss[i]<='Z')||(ss[i]>='a'&&ss[i]<='z')||(ss[i]>=''&&ss[i]<=''))

         {

               break;

         }

         else i++;

      }

    }

 }

 void fun2(string ss)

 {

      int i=;string tem="";

                 while(i<ss.length())

        {

                 while(i<ss.length())

             {

                if((ss[i]>='A'&&ss[i]<='Z')||(ss[i]>='a'&&ss[i]<='z')||(ss[i]>=''&&ss[i]<=''))

                      break;

                   else i++;

            }

              while(i<ss.length())

            {

               if((ss[i]>='A'&&ss[i]<='Z')||(ss[i]>='a'&&ss[i]<='z')||(ss[i]>=''&&ss[i]<=''))

                {

                 if(ss[i]>='A'&&ss[i]<='Z')

                       ss[i]=ss[i]-'A'+'a';

                    tem+=ss[i];

                     ++i;

                 }

                else break;

             }

            ++mm[tem].time;

                mm[tem].len=i;

             tem="";  

          }

    }

 int main()

 {

       string ss;

       int i,j;int c1,c2;

      while(getline(cin,ss))

       {

             mm.clear();

             if((ss[]>='A'&&ss[]<='Z')||(ss[]>='a'&&ss[]<='z')||(ss[]>=''&&ss[]<=''))

                   fun1(ss);

             else fun2(ss);

         int Max=;int Min=;

                   string most;

               map<string,word>::iterator it;

               for(it=mm.begin();it!=mm.end();it++)

               {                    if((it->second).time>Max||((it->second).time==Max&&(it->second).len<Min))

                   {

                      Max=(it->second).time;

                      Min=(it->second).len;

                most=it->first;

                   }

               }

         cout<<most<<" "<<Max<<endl;

       }

    return ;

 }

Speech Patterns (string)的更多相关文章

  1. Pat1071: Speech Patterns

    1071. Speech Patterns (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HOU, Qiming Peo ...

  2. 【算法笔记】A1071 Speech Patterns

    1071 Speech Patterns (25 分)   People often have a preference among synonyms of the same word. For ex ...

  3. PAT 1071 Speech Patterns[一般]

    1071 Speech Patterns (25 分) People often have a preference among synonyms of the same word. For exam ...

  4. PAT 甲级 1071 Speech Patterns (25 分)(map)

    1071 Speech Patterns (25 分)   People often have a preference among synonyms of the same word. For ex ...

  5. PAT_A1071#Speech Patterns

    Source: PAT A1071 Speech Patterns (25 分) Description: People often have a preference among synonyms ...

  6. 1071 Speech Patterns——PAT甲级真题

    1071 Speech Patterns People often have a preference among synonyms of the same word. For example, so ...

  7. 1071. Speech Patterns (25)

    People often have a preference among synonyms of the same word. For example, some may prefer "t ...

  8. A1071. Speech Patterns

    People often have a preference among synonyms of the same word. For example, some may prefer "t ...

  9. 1071 Speech Patterns

    People often have a preference among synonyms of the same word. For example, some may prefer "t ...

随机推荐

  1. Android之Handler探索

    Handler背景理解: Handler被最多的使用在了更新UI线程中,但是,这个方法具体是什么样的呢?我在这篇博文中先领着大家认识一下什么是handler以及它是怎么样使用在程序中,起着什么样的作用 ...

  2. javaweb学习总结十九(http协议概述以及http请求信息分析)

    一:http协议概述 1:http协议定义 2:使用telnet程序连接web服务器,获取资源 3:http协议:超文本传输协议,它是基于tcp/ip协议,就是建立在tcp/ip之上工作的, http ...

  3. Java计算文件的SHA码和MD5码

    可参考:http://blog.csdn.net/hudashi/article/details/8394158 /** * 计算文件的MD5码 * @param file * @return */ ...

  4. poisspdf(so also poisscdf, poissfit, poissinv, poissrnd, poisstat, pdf.)

    Poisson分布的累积概率值 命令:poisscdf 格式:poisscdf (k, Lambda) Poisson分布 在二项分布中,当n的值很大,p的值很小,而np又较适中时,用Poisson分 ...

  5. AppScan安全问题解决方案

    一. 环境准备 测试通常给的是PDF文档,动辄几百页,看起来很费劲,看文档的时间可能比解决问题的时间还长...所以作为需要解决问题的我们来说,最好安装AppScan,请测试人员提供类型为AppScan ...

  6. MVC下用户登录状态校验的问题以及解决方案--------------Action全局过滤器的使用

    前言当我们访问某个网站的时候需要检测用户是否已经登录(通过Session是否为null),我们知道在WebForm中可以定义一个BasePage类让他继承System.Web.UI.Page,重写它的 ...

  7. 省市数据递归加载到TreeView

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. Jquery插件的编写和使用

    第七章 Jquery插件的编写和使用    插件的定义: 插件也称为扩展,是一种遵循一定规范的应用程序接口编写出来的程序. 下面是Jquery插件的编写很使用:要查看请点击:Jquery插件的编写很使 ...

  9. MD5和Base64介绍与应用

    MD5:概念:MD5是一种不可逆的消息摘要算法.为计算机安全领域广泛使⽤的一种散列函数, 用以提供消息的完整性保护.效果:把一个任意长度的字节串变换成⼀定⻓度的⼗六进制数字串. 目的是让⼤容量信息在⽤ ...

  10. IMDb Search

    主要是一个搜索电影,显示电影信息的小程序,开发坏境是xcode7.0,swift2.0 创建HTTP请求连接并对连接格式化 了解NSURLSession类的使用方法 通过NSURLSession类的d ...