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. log4j个人使用整理

    Log4j介绍: 略过. 配置: Eclipse项目中添加log4j.jar到lib下. 在bin目录下新建log4j.properties,编辑好log4j配置文件. 样例分析: log4j.roo ...

  2. json2使用方法

    例子1: //直接声明json数据结构 var myJSONObject = {"bindings": [ {"ircEvent": "PRIVMSG ...

  3. Java作业代写

    作业一 试用java编写一个九九乘法表并打印. 作业二: 设计两个人类与书类,并设置两者的关系,试用人去找书,书去找人,假如某人有一个儿子,它也有一本书,试用儿子去找书,书找儿子. 大作业 熟悉QQ农 ...

  4. Delphi 获取进程路径及命令行参数

    Delphi 获取进程路径及命令行参数, 但有的进程获取时会报错,不知为啥 type PVOID64 = UINT64; _UNICODE_STRING = packed record Length ...

  5. 关于Tesseract3.01的使用方法

    Tesseract就不多介绍勒,能找到的人都知道是干嘛的 下面记录一下C# vs2010下的使用方法(借鉴http://blog.csdn.net/bobo1013767522/article/det ...

  6. bootstrap学习起步篇:初识bootstrap之html5语法构建hello篇(一)

    目前选择使用bootstrap作为前端页面模板,是件很省心的事情.官网上给出的实例和教程也很多.在实际使用过程中,我们也许还要借助文档去了解它的元素和样式.但也不能减少我们使用他的兴趣. 我准备将其整 ...

  7. CSS之生成全屏背景图片

    在CSS中背景图片的填充方法: background-position:x,y(背景图片x,y轴的定位) background-repeat:no-repeat(不平铺) background-rep ...

  8. Android之记账本

    这个ColaBox记事本是我从网上下载下来的拿来学习一下的(APK下载点这里.) 从登记收入与开支的页面跳转到账单页面运用了SQL数据库的录入,整体表的结构为: db.execSQL("CR ...

  9. Cocos中的观察者设计模式与通知机制

    观察者(Observer)模式也叫发布/订阅(Publish/Subscribe)模式,是 MVC( 模型-视图-控制器)模式的重要组成部分.天气一直是英国人喜欢讨论的话题,而最近几年天气的变化也成为 ...

  10. LINQ(隐式表达式、lambda 表达式)

    .NET 中一项突破性的创新是 LINQ(Language Integrated Query,语言集成查询),这组语言扩展让你能够不必离开舒适的 C# 语言执行查询. LINQ 定义了用于构建查询表达 ...