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. 一网打尽OkHttp中的缓存问题

    看到很多小伙伴对OkHttp的缓存问题并不是十分了解,于是打算来说说这个问题.用好OkHttp中提供的缓存,可以帮助我们更好的使用Retrofit.Picasso等配合OkHttp使用的框架.OK,废 ...

  2. Java并发包学习--ReentrantLock

    这个锁叫可重入锁.它其实语义上和synchronized差不多,但是添加了一些拓展的特性. A reentrant mutual exclusion Lock with the same basic ...

  3. 1. Android 系统上一款开源的图表库

    1. MPAndroidChart  MPAndroidChart 是 Android 系统上一款开源的图表库.目前提供线图和饼图,支持选择.缩放和拖放. 一个可以拖动缩放的图表库,包含曲线图.直方图 ...

  4. ie6 7 8 9 firefox的css兼容问题

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. python(6)-logging 日志模块

    logging的日志分为5个级别分别为debug(), info(), warning(), error(), critical() 先来看一下简单的代码: logging.basicConfig(f ...

  6. flex数据交互方式 转

    Flex数据交互方法- httpservice, webservice, RemoteObject, socket. 写在前面: 使用SOAP Web Service同Flex交互有很多好处,但是它很 ...

  7. [译]脱离jQuery,使用原生Ajax

    脱离jQuery,使用原生Ajax 标签: Ajax translate 英文出处:<A Guide to Vanilla Ajax Without jQuery> 翻译: 刘健超 J.c ...

  8. 【AngularJs】---Error: [$injector:modulerr] Failed to instantiate module starter.services

    [遇到问题解决问题,原谅我这个菜鸟] 加了services angular.module('starter', ['ionic', 'starter.controllers', 'starter.se ...

  9. 【转载】Apache Kafka:下一代分布式消息系统

    http://www.infoq.com/cn/articles/kafka-analysis-part-1 Kafka是由LinkedIn开发的一个分布式的消息系统,使用Scala编写,它以可水平扩 ...

  10. 【转】Github入门教程

    原文 http://www.eoeandroid.com/thread-274556-1-1.html [初识Github] 首先让我们大家一起喊一句“Hello Github”.YEAH!就是这样. ...