Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. PCMS Web Client
  2. ИнформацияРезультатыОтправитьРешенияВопросыФайлыВыход
  3. Исходный код
  4. ConsoleApplication7.cpp
  5. #include <iostream>
  6. #include <fstream>
  7. #include <vector>
  8. #include <string>
  9. using namespace std;
  10. struct map {
  11. string key;
  12. string value;
  13. map *next = NULL;
  14. map *prev = NULL;
  15. };
  16. int myhash(string s)
  17. {
  18. int ans = 0;
  19. for (int i = 0; i < s.size(); i++)
  20. {
  21. ans *= 53;
  22. ans += s[i];
  23. ans %= 1000003;
  24. }
  25. return ans;
  26. }
  27. int main()
  28. {
  29. ifstream cin("linkedmap.in");
  30. ofstream cout("linkedmap.out");
  31. string s, s1, s2;
  32. vector <vector<map>> a(1000003);// , vector<map>(100))
  33. //vector <int> b(1000003, 0);
  34. int t;
  35. bool flag;
  36. map *prev = NULL;
  37. while (cin >> s)
  38. {
  39. if (s[0] == 'p'&&s.size() == 3)
  40. {
  41. cin >> s1 >> s2;
  42. t = myhash(s1);
  43. flag = false;
  44. for (int i = 0; i < a[t].size(); i++)
  45. {
  46. if (a[t][i].key == s1)
  47. {
  48. flag = true;
  49. a[t][i].value = s2;
  50. }
  51. }
  52. if (flag == false)
  53. {
  54. if (a[t].size() == 0)
  55. a[t].reserve(100);
  56. map x;
  57. x.key = s1;
  58. x.value = s2;
  59. x.next = NULL;
  60. x.prev = prev;
  61. a[t].push_back(x);
  62. if(prev!=NULL)
  63. prev->next= &a[t][a[t].size() - 1];
  64. prev = &a[t][a[t].size() - 1];
  65.  
  66. }
  67. }
  68. else if (s[0] == 'd')
  69. {
  70. cin >> s1;
  71. t = myhash(s1);
  72. for (int i = 0; i < a[t].size(); i++)
  73. {
  74. if (a[t][i].key == s1)
  75. {
  76. if (a[t][i].next == NULL)
  77. prev = a[t][i].prev;
  78. else
  79. a[t][i].next->prev = a[t][i].prev;
  80. if (a[t][i].prev != NULL)
  81. a[t][i].prev->next = a[t][i].next;
  82. a[t][i].value ="";
  83. a[t][i].next = NULL;
  84. a[t][i].prev = NULL;
  85. a[t][i].key ="";
  86. break;
  87. }
  88. }
  89.  
  90. }
  91. else if (s[0] == 'g')
  92. {
  93. cin >> s1;
  94. t = myhash(s1);
  95. flag = false;
  96. for (int i = 0; i < a[t].size(); i++)
  97. {
  98. if (s1 == a[t][i].key)
  99. {
  100. cout << a[t][i].value << endl;
  101. flag = true;
  102. }
  103. }
  104. if (flag == false) cout << "none\n";
  105. }
  106. else if (s[0] == 'p'&&s.size() == 4)
  107. {
  108. cin >> s1;
  109. t = myhash(s1);
  110. flag = false;
  111. for (int i = 0; i < a[t].size(); i++)
  112. {
  113. if (s1 == a[t][i].key)
  114. {
  115. if (a[t][i].prev != NULL) {
  116. cout << a[t][i].prev->value << endl;
  117. flag = true;
  118. }
  119. }
  120. }
  121. if (flag == false) cout << "none\n";
  122. }
  123. else if (s[0] == 'n')
  124. {
  125. cin >> s1;
  126. t = myhash(s1);
  127. flag = false;
  128. for (int i = 0; i < a[t].size(); i++)
  129. {
  130. if (s1 == a[t][i].key)
  131. {
  132. if (a[t][i].next != NULL) {
  133. cout << a[t][i].next->value << endl;
  134. flag = true;
  135. }
  136. }
  137. }
  138. if (flag == false) cout << "none\n";
  139. }
  140. }
  141. return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement