Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <map>
  6.  
  7. using namespace std;
  8.  
  9. vector<string> intersection(vector<string> &v1, vector<string> &v2) {
  10. vector<string> v3;
  11. sort(v1.begin(), v1.end());
  12. sort(v2.begin(), v2.end());
  13. set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v3));
  14. return v3;
  15. }
  16.  
  17. vector<string>unite(vector<string> &v1,vector<string> &v2) {
  18. sort(v1.begin(), v1.end());
  19. sort(v2.begin(), v2.end());
  20. vector<string> v3;
  21. set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));
  22. return v3;
  23. }
  24.  
  25. int main() {
  26. string s_file, s_console, line_file;
  27. string loc;
  28. ifstream infile("C:\\\\Users\\\\ivanf\\\\CLionProjects\\\\serv2\\\\count_big.txt");
  29. //ifstream infile("count_big.txt");
  30. bool first_time = true;
  31. int count = 0;
  32. map<string, vector<string>> v_file;
  33. map<string, int> freq;
  34. while (cin >> s_console) {
  35. vector<string> v_console;
  36. if (s_console.length() == 1)
  37. v_console.push_back(s_console);
  38. else {
  39. for (int i = 0; i < s_console.length() - 1; i++) {
  40. v_console.push_back(s_console.substr(i, 2));
  41. }
  42. }
  43. float fraction = 0;
  44. int frequnt = 0;
  45. string word = "zzz";
  46. int i = 0;
  47. int num;
  48. if (first_time) {
  49. while (getline(infile, loc)) {
  50. s_file = loc.substr(0, loc.find('\t'));
  51. num = stoi(loc.substr(loc.find('\t')));
  52. s_file;
  53. freq[s_file] = num;
  54. vector<string> temp_file;
  55. if (s_file.length() == 1)
  56. temp_file.push_back(s_file);
  57. else {
  58. for (int i = 0; i < s_file.length() - 1; i++) {
  59. temp_file.push_back(s_file.substr(i, 2));
  60. }
  61. }
  62. v_file[s_file] = temp_file;
  63. vector<string> intersec = intersection(v_console, temp_file);
  64. vector<string> assoc = unite(v_console, temp_file);
  65. if ((float) intersec.size() / (float) assoc.size() > fraction) {
  66. fraction = (float) intersec.size() / (float) assoc.size();
  67. frequnt = num;
  68. word = s_file;
  69. } else if ((float) intersec.size() / (float) assoc.size() == fraction) {
  70. if (num > frequnt) {
  71. fraction = (float) intersec.size() / (float) assoc.size();
  72. frequnt = num;
  73. word = s_file;
  74. } else if (num == frequnt) {
  75. if (s_file < word) {
  76. fraction = (float) intersec.size() / (float) assoc.size();
  77. frequnt = num;
  78. word = s_file;
  79. }
  80. }
  81. }
  82. i++;
  83. count++;
  84. }
  85. } else {
  86. //for (int i = 0; i < count; i++)
  87. for (auto temp : v_file) {
  88. vector<string> intersec = intersection(v_console, temp.second);
  89. vector<string> assoc = unite(v_console, temp.second);
  90. if ((float) intersec.size() / (float) assoc.size() > fraction) {
  91. fraction = (float) intersec.size() / (float) assoc.size();
  92. frequnt = freq[temp.first];
  93. word = temp.first;
  94. } else if ((float) intersec.size() / (float) assoc.size() == fraction) {
  95. if (freq[temp.first] > frequnt) {
  96. fraction = (float) intersec.size() / (float) assoc.size();
  97. frequnt = freq[temp.first];
  98. word = temp.first;
  99. } else if (freq[temp.first] == frequnt) {
  100. if (temp.first < word) {
  101. fraction = (float) intersec.size() / (float) assoc.size();
  102. frequnt = freq[temp.first];
  103. word = temp.first;
  104. }
  105. }
  106. }
  107. }
  108. }
  109. first_time = false;
  110. cout << word << endl;
  111. }
  112. return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement