fork download
  1. //(c)Terminator
  2. #include <iostream>
  3. #include <cctype>
  4. #include <cstring>
  5. #include <string>
  6. #include <map>
  7. using namespace std;
  8.  
  9. struct scmp {
  10. bool operator () (const string& a, const string& b) const {
  11. return (strcmp(a.c_str(), b.c_str()) < 0);
  12. }
  13. };
  14.  
  15. typedef map<string, int, scmp> words;
  16.  
  17.  
  18. int maxrepeat_word(const char* s, string& d){
  19. words::iterator pw;
  20. const char* p;
  21. string w;
  22. words ws;
  23. int n = 0;
  24.  
  25. for(d = ""; *s; s = p) {
  26.  
  27. while(*s && ! isalpha(*s))
  28. ++s;
  29. if(! *s)
  30. break;
  31. for(p = s; isalpha(*p); )
  32. ++p;
  33.  
  34. w.assign(s, p);
  35. if((pw = ws.find(w)) != ws.end()){
  36. if(++(pw->second) > n){
  37. d = w;
  38. n = pw->second;
  39. }
  40. } else
  41. ws.insert(make_pair(w, 1));
  42. }
  43.  
  44. ws.clear();
  45. return n;
  46. }
  47.  
  48.  
  49.  
  50. int main(void){
  51. char s[] = "dog, fox, [dog], fox, wolf-dog, cat, dog";
  52.  
  53. string d;
  54. int n = maxrepeat_word(s, d);
  55. cout << "word: " << d << endl;
  56. cout << "count: " << n << endl;
  57. return 0;
  58. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
word:  dog
count: 4