fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <fstream>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <set>
  7. #include <map>
  8. #include <utility>
  9. #include <cctype>
  10.  
  11. std::string toLower(const std::string& input);
  12. std::string stripSpaces(const std::string& input);
  13. std::string stripPunctuation(const std::string& input);
  14. std::vector<std::string> bySentence(const std::string& text);
  15. std::map<std::string, int> countWords(const std::string& text);
  16. size_t countOccurrences(const std::string& haystack, const std::string& needle);
  17.  
  18. const std::set<std::string> ignore {"a", "about", "above", "after", "again", "against", "all", "am", "an", "and", "any", "are", "aren't", "as", "at", "be", "because", "been", "before", "being", "below", "between", "both", "but", "by", "can't", "cannot", "could", "couldn't", "did", "didn't", "do", "does", "doesn't", "doing", "don't", "down", "during", "each", "few", "for", "from", "further", "had", "hadn't", "has", "hasn't", "have", "haven't", "having", "he", "he'd", "he'll", "he's", "her", "here", "here's", "hers", "herself", "him", "himself", "his", "how", "how's", "i", "i'd", "i'll", "i'm", "i've", "if", "in", "into", "is", "isn't", "it", "it's", "its", "itself", "let's", "me", "more", "most", "mustn't", "my", "myself", "no", "nor", "not", "of", "off", "on", "once", "only", "or", "other", "ought", "our", "ours", "ourselves", "out", "over", "own", "same", "shan't", "she", "she'd", "she'll", "she's", "should", "shouldn't", "so", "some", "such", "than", "that", "that's", "the", "their", "theirs", "them", "themselves", "then", "there", "there's", "these", "they", "they'd", "they'll", "they're", "they've", "this", "those", "through", "to", "too", "under", "until", "up", "very", "was", "wasn't", "we", "we'd", "we'll", "we're", "we've", "were", "weren't", "what", "what's", "when", "when's", "where", "where's", "which", "while", "who", "who's", "whom", "why", "why's", "with", "won't", "would", "wouldn't", "you", "you'd", "you'll", "you're", "you've", "your", "yours", "yourself", "yourselves"};
  19.  
  20. int main()
  21. {
  22. std::string text;
  23. getline(std::cin, text);
  24. auto sentences = bySentence(text);
  25. auto wordCount = countWords(text);
  26.  
  27. std::vector<std::pair<std::string, int>> mostCommon;
  28. mostCommon.reserve(wordCount.size());
  29. for(const auto& kv: wordCount)
  30. mostCommon.push_back(kv);
  31. std::sort(mostCommon.begin(), mostCommon.end(), [](std::pair<std::string, int> a, std::pair<std::string, int> b) {
  32. return a.second > b.second;
  33. });
  34.  
  35. std::sort(sentences.begin(), sentences.end(), [mostCommon](const std::string& a, const std::string& b) {
  36. return countOccurrences(a, mostCommon[0].first) > countOccurrences(b, mostCommon[0].first);
  37. });
  38.  
  39. std::cout << sentences[0] << std::endl << std::endl;
  40. std::cout << sentences[1] << std::endl << std::endl;
  41. }
  42.  
  43. size_t countOccurrences(const std::string& haystack, const std::string& needle)
  44. {
  45. std::stringstream ss(haystack);
  46. std::string word;
  47. size_t count = 0;
  48. while(ss >> word)
  49. if(toLower(stripPunctuation(word)) == needle)
  50. count++;
  51. return count;
  52. }
  53.  
  54. std::map<std::string, int> countWords(const std::string& text)
  55. {
  56. std::map<std::string, int> wordCount;
  57. std::stringstream ss(text);
  58. std::string word;
  59. while(ss >> word)
  60. {
  61. word = stripPunctuation(toLower(word));
  62. if(ignore.find(word) == ignore.end())
  63. wordCount[word]++;
  64. }
  65. return wordCount;
  66. }
  67.  
  68. std::string toLower(const std::string& input)
  69. {
  70. std::string result = input;
  71. std::transform(result.begin(), result.end(), result.begin(), ::tolower);
  72. return result;
  73. }
  74.  
  75. std::vector<std::string> bySentence(const std::string& text)
  76. {
  77. std::vector<std::string> sentences;
  78. size_t start = 0;
  79. size_t period = text.find('.', start);
  80. if(period == std::string::npos)
  81. {
  82. sentences.push_back(stripSpaces(text));
  83. return sentences;
  84. }
  85.  
  86. while(true)
  87. {
  88. sentences.push_back(stripSpaces(text.substr(start, period - start + 1)));
  89. start = period + 1;
  90. period = text.find('.', start);
  91. if(period == std::string::npos)
  92. break;
  93. }
  94.  
  95. return sentences;
  96. }
  97.  
  98. std::string stripSpaces(const std::string& input)
  99. {
  100. size_t start = 0, stop = input.length() - 1;
  101. while(input[stop] == ' ')
  102. stop--;
  103. while(input[start] == ' ')
  104. start++;
  105. return input.substr(start, stop - start + 1);
  106. }
  107.  
  108. std::string stripPunctuation(const std::string& input)
  109. {
  110. if(all_of(input.begin(), input.end(), ::isalnum))
  111. return input;
  112. size_t start = 0, stop = input.length() - 1;
  113. while(!isalnum(input[stop]))
  114. stop--;
  115. while(!isalnum(input[start]))
  116. start++;
  117. return input.substr(start, stop - start + 1);
  118. }
Success #stdin #stdout 0s 15280KB
stdin
This case describes the establishment of a new Cisco Systems R&D facility in Shanghai, China, and the great concern that arises when a collaborating R&D site in the United States is closed down. What will that closure do to relationships between the Shanghai and San Jose business units? Will they be blamed and accused of replacing the US engineers? How will it affect other projects? The case also covers aspects of the site's establishment, such as securing an appropriate building, assembling a workforce, seeking appropriate projects, developing managers, building teams, evaluating performance, protecting intellectual property, and managing growth. Suitable for use in organizational behavior, human resource management, and strategy classes at the MBA and executive education levels, the material dramatizes the challenges of changing a US-based company into a global competitor.
stdout
What will that closure do to relationships between the Shanghai and San Jose business units? Will they be blamed and accused of replacing the US engineers? How will it affect other projects? The case also covers aspects of the site's establishment, such as securing an appropriate building, assembling a workforce, seeking appropriate projects, developing managers, building teams, evaluating performance, protecting intellectual property, and managing growth.

This case describes the establishment of a new Cisco Systems R&D facility in Shanghai, China, and the great concern that arises when a collaborating R&D site in the United States is closed down.