Advertisement
mskf

Untitled

Jun 1st, 2020
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <iterator>
  3. #include <map>
  4. #include <cctype>
  5. #include <algorithm>
  6. #include <sstream>
  7. #include <vector>
  8. #include <set>
  9. using namespace std;
  10.  
  11. const string content = "In Xanadu did Kubla Khan\n"
  12. "A stately pleasure-dome decree:\n"
  13. "Where Alph, the sacred river, ran\n"
  14. "Through caverns measureless to man\n"
  15. "Down to a sunless sea.\n"
  16. "\n"
  17. "So twice five miles of fertile ground\n"
  18. "With walls and towers were girdled round:\n"
  19. "And there were gardens bright with sinuous rills,\n"
  20. "Where blossomed many an incense-bearing tree;\n"
  21. "And here were forests ancient as the hills,\n"
  22. "Enfolding sunny spots of greenery.\n"
  23. "\n"
  24. "But oh! that deep romantic chasm which slanted\n"
  25. "Down the green hill athwart a cedarn cover!\n"
  26. "A savage place! as holy and enchanted\n"
  27. "As e'er beneath a waning moon was haunted\n"
  28. "By woman wailing for her demon-lover\n";
  29.  
  30. vector<string> split(const string & content, char spliter){
  31. stringstream ss(content);
  32. string item;
  33. vector<string> res;
  34. while(getline(ss,item,spliter)){
  35. res.push_back(item);
  36. }
  37. return res;
  38. }
  39.  
  40. string getWord(const string & str){
  41. string nstr = "";
  42. for(const char &c: str){
  43. if(!ispunct(c))
  44. nstr+=tolower(c);
  45. };
  46. return nstr;
  47. }
  48.  
  49. int main()
  50. {
  51. float threshold = 0.028;
  52. map<string, set<int>> gquiz1;
  53.  
  54. vector<string> words;
  55. vector<string> lines = split(content,'\n');
  56. int linenum = 1;
  57. int wordCount = 0;
  58. for (string line: lines){
  59. line.erase(remove(line.begin(), line.end(), '\r'), line.end());
  60. vector<string> _words = split(line,' ');
  61. for(string word:_words){
  62. wordCount++;
  63. word = getWord(word);
  64. if(word.size()>0){
  65. if(gquiz1.find(word) == gquiz1.end()){
  66. set<int> _set;
  67. _set.insert(linenum);
  68. gquiz1[word] = _set;
  69.  
  70. }else{
  71. gquiz1[word].insert(linenum);
  72. }
  73. }
  74. }
  75. linenum++;
  76. }
  77.  
  78. int num;
  79. map<string, set<int>>::iterator itr;
  80. for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr) {
  81. if (((float)(itr->second.size())/(float)wordCount)<threshold){
  82. cout << itr->first<<": ";
  83. set<int> _set = itr->second;
  84. for(auto it=_set.begin();it!=_set.end();it++){
  85. if (next(it)!=_set.end()){
  86. cout<<*it<<' ';
  87. }else{
  88. cout<<*it;
  89. }
  90. }
  91. cout<<endl;
  92. }
  93. }
  94.  
  95. cout << endl;
  96. return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement