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. SRM 584 第一次玩TopCoder。。。只水题一道。。。

    第一次topcoder,以前老感觉没有资格去做tc,cf什么的,现在已经慢慢接触了. 感觉还可以,还是有让我们这些蒻菜安慰的水题. tc的确很好玩,用客户端比赛,还有各种规则,而且还是只编写一个类提交 ...

  2. ResponseBody的使用

    使用Spring的@ResponseBody有时还是挺方便的,在ajax调用返回纯字符串时有中文编码问题. @ResponseBody @RequestMapping(value="/dec ...

  3. C&python文件读写效率

    不同缓冲区读写文件的效率比较 环境:CentOS6.3/ext3/ 读取文件大小:100000000B BUFSIZE TIMECOST 1 real 0m38.909s user 0m5.960s  ...

  4. fatal error LNK1168: cannot open Debug/opreat.exe for writing

    问题:LINK : fatal error LNK1168: cannot open Debug/opreat.exe for writing           Error executing li ...

  5. C#控制台程序 使用 Server.MapPath,

    (1)添加引用 System.Web. (2)在类中填写 using System.Web 命名空间. (3)写法为: System.Web.HttpContext.Current.Server.Ma ...

  6. java演示facade(外观)模式

    实际应用中,原来的代码涉及多个子系统时,重新进行类的设计,将原来分散在源码中的类结构及方法重新组合,形成新的.统一的接口,供上层应用使用. Facade所面对的往往是多个类或其它程序单元,通过重新组合 ...

  7. Java Concurrency - ThreadFactory, 使用工厂方法创建线程

    当需要创建多个类似的线程实例时,使用工厂模式替代 new 操作符创建线程,能使代码更为简洁,易于维护.JDK 提供了 java.util.concurrent.ThreadFactory 接口,Thr ...

  8. centos5.6部署gcc4.7编译的程序导致问题

    因为用了c++0x的一些新特性,必须使用4.6及以上的版本编译,所以使用了4.7编译,运行时提示错误 libstdc++.so.6(GLIBCXX_3.4.14) 错误 这个时候下了个glibc2.7 ...

  9. oracle trunc 函数处理日期格式

    select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;  --显示当前时间 2011-12-29 16:24:34 select trun ...

  10. DayDream, 移动VR 2.0里程碑: 概述(上篇)

    VR设备, 断断续续使用了很多个; 尤其是最近半年,主要是PC VR方面项目. 以前对移动VR不感冒,这几天试用了一下DayDream, 眼前突然一亮, 就如同年初首次使用HTC Vive眼前一亮的感 ...