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. 前端必会html知识整理

    1.浏览器内核:         1.ie:trident(三叉戟)内核         2.firefox:gecko(壁虎)内核         3.safari:webkit(浏览器核心)内核 ...

  2. Java中的文件操作

    在使用计算机编程中,常常会用到对于文件的操作,以下是我对于Java中文件的相关内容学习之后的一个总结和在学习过程中遇到的一些问题. 一.什么是文件 对于文件进行操作,首先我们要知道什么是文件.在此之前 ...

  3. poj 2942 点的双连通分量

    思路: 对于该图,直接用建图貌似没法解,所以也很容易想到建补图,这样存在边的两个点就能再圆桌上做一起.也就将问题转化为对双连通分量中是否存在奇圈了. 我们将每次查询的边保存在stack中,当遇到关键点 ...

  4. Windows 8.1 归档 —— Step 2 对新系统的少量优化

    下面是来自 iplaysoft 的优化技巧:

  5. HTML5与CSS3基础教程第八版学习笔记7~10章

    第七章,CSS构造块 CSS里有控制基本格式的属性(font-size,color),有控制布局的属性(position,float),还有决定访问者打印时在哪里换页的打印控制元素.CSS还有很多控制 ...

  6. Java Concurrency - Concurrent Collections

    Data structures are a basic element in programming. Almost every program uses one or more types of d ...

  7. Tomcat - JNDI 配置

    1. Create Your JavaBean Class Create the JavaBean class which will be instantiated each time that th ...

  8. HDOJ2024C语言合法标识符

    C语言合法标识符 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  9. iOS 在viewController中监听Home键触发以及重新进入界面的方法

    第一步:创建2个NSNotificationCenter监听 [[NSNotificationCenter defaultCenter] addObserver:self selector:@sele ...

  10. iPad accessory communication through UART

    We manufacture a new accessory for iPad/iPhone which should transfer commands to the iPad. We like t ...