码迷,mamicode.com
首页 > 其他好文 > 详细

1071. Speech Patterns (25)

时间:2015-12-06 13:03:46      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

map的使用

时间限制
300 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
HOU, Qiming

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
 

  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5. #include <algorithm>
  6. #include <map>
  7. #pragma warning(disable:4996)
  8. using namespace std;
  9. map<string, int> mp;
  10. bool CheckChar(char a) {
  11. if (a >= ‘0‘&&a <= ‘9‘)
  12. return true;
  13. else if (a >= ‘a‘&&a <= ‘z‘)
  14. return true;
  15. else if (a >= ‘A‘&&a <= ‘Z‘)
  16. return true;
  17. else
  18. return false;
  19. }
  20. int main(void) {
  21. string wordTemp;
  22. char ab;
  23. bool stopFlag = false;
  24. string maxS;
  25. int max = 0;
  26. while (true)
  27. {
  28. if (stopFlag == true)
  29. break;
  30. scanf("%c", &ab);
  31. if (ab == ‘\n‘)
  32. stopFlag = true;
  33. if (CheckChar(ab)==true) {
  34. if (ab >= ‘A‘&&ab <= ‘Z‘)
  35. wordTemp += ab - ‘A‘ + ‘a‘;
  36. else
  37. wordTemp += ab;
  38. }
  39. else {
  40. if (wordTemp == "")
  41. continue;
  42. else {
  43. bool haveThisWord = false;
  44. mp[wordTemp]++;
  45. if (mp[wordTemp] > max) {
  46. max = mp[wordTemp];
  47. maxS = wordTemp;
  48. }
  49. wordTemp = "";
  50. }
  51. }
  52. if (stopFlag == true)
  53. break;
  54. }
  55. cout << maxS << " " << max << endl;
  56. return 0;
  57. }





1071. Speech Patterns (25)

标签:

原文地址:http://www.cnblogs.com/zzandliz/p/5023228.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!