1071. Speech Patterns (25)

约定不等于承诺〃 2022-05-30 01:46 218阅读 0赞

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:

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

Sample Output:

  1. can 5

题目大意:

代码:

  1. #include<stdio.h>
  2. #include<map>
  3. #include<string>
  4. #include<iostream>
  5. using namespace std;
  6. int main()
  7. {
  8. int i,j,Max;
  9. string s,t;
  10. map<string,int> Map;
  11. getline(cin,s);
  12. for(i=0;i<s.length();i++)
  13. {
  14. if(isalnum(s[i]))
  15. {
  16. s[i]=tolower(s[i]);
  17. t+=s[i];
  18. }
  19. if(!isalnum(s[i])||i==s.length()-1)
  20. {
  21. if(t.length()!=0)
  22. {
  23. Map[t]++;
  24. t="";
  25. }
  26. }
  27. }
  28. Max=0;
  29. map<string,int>::iterator it;
  30. for(it=Map.begin();it!=Map.end();it++)
  31. {
  32. if(it->second>Max)
  33. {
  34. Max=it->second;
  35. t=it->first;
  36. }
  37. }
  38. cout<<t<<' '<<Max<<endl;
  39. return 0;
  40. }

发表评论

表情:
评论列表 (有 0 条评论,218人围观)

还没有评论,来说两句吧...

相关阅读

    相关 mysql 1071错误

    mysql 1071错误经过查询才知道,是Mysql的字段设置的太长了,于是我把这两个字段的长度改了一下就好了。  建立索引时,数据库计算key的长度是累加所有Ind