Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <vector>
  5. #include <string>
  6. using namespace std;
  7. int myhash(string s)
  8. {
  9. int ans = 0;
  10. for (int i = 0; i < s.size(); i++)
  11. {
  12. ans *= 53;
  13. ans += s[i];
  14. ans %= 1000003;
  15. }
  16. return ans;
  17. }
  18. int main()
  19. {
  20. ifstream cin("multimap.in");
  21. ofstream cout("multimap.out");
  22. string s, s1, s2;
  23. vector <vector<pair<string,vector<string>>>> a(1e6 + 3);
  24. int t;
  25. bool flag,flag1;
  26. while (cin >> s)
  27. {
  28. if (s[0] == 'p')
  29. {
  30. cin >> s1 >> s2;
  31. t = myhash(s1);
  32. flag1 = false;
  33. for (int i = 0; i < a[t].size(); i++)
  34. {
  35. if (s1 == a[t][i].first) {
  36. flag = false;
  37. for (int j = 0; j < a[t][i].second.size();j++)
  38. {
  39. if (a[t][i].second[j] == s2) {
  40. flag = true; flag1 = true;
  41. }
  42. }
  43. if (flag == false) {
  44. a[t][i].second.push_back(s2);
  45. flag1 = true;
  46. }
  47. }
  48. }
  49. if (flag1 == false)
  50. a[t].push_back(make_pair(s1, vector<string>(1, s2)));
  51. }
  52. else if (s[0] == 'g')
  53. {
  54. cin >> s1;
  55. t= myhash(s1);
  56. flag = false;
  57. for (int i = 0; i < a[t].size(); i++)
  58. {
  59. if (s1 == a[t][i].first) {
  60. flag = true;
  61. cout << a[t][i].second.size() << ' ';
  62. for (int j = 0; j < a[t][i].second.size(); j++)
  63. cout << a[t][i].second[j] << ' ';
  64. cout << endl;
  65. }
  66. }
  67. if (flag == false) cout << 0 << endl;
  68. }
  69. else if (s[0] == 'd')
  70. {
  71. if (s.size() == 6) // delete
  72. {
  73. cin >> s1 >> s2;
  74. t = myhash(s1);
  75. for (int i = 0; i < a[t].size(); i++)
  76. {
  77. if (s1 == a[t][i].first) {
  78. for (auto j = a[t][i].second.begin(); j != a[t][i].second.end(); j++)
  79. {
  80. if (*j == s2)
  81. {
  82. a[t][i].second.erase(j);
  83. break;
  84. }
  85. }
  86. }
  87. }
  88. }
  89. else //deleteall
  90. {
  91. cin >> s1;
  92. t = myhash(s1);
  93. for (auto i = a[t].begin(); i != a[t].end(); i++)
  94. {
  95. if (s1 == i->first) {
  96. a[t].erase(i);
  97. break;
  98. }
  99. }
  100. }
  101. }
  102. }
  103. return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement