Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define int int64_t
  3. //GCC optimize ("O3");
  4.  
  5. using namespace std;
  6. typedef long long ll;
  7. typedef pair <string, string> pss;
  8. typedef vector <pss> vpss;
  9. int n, m;
  10. vpss v;
  11. map <string, int> cnt;
  12. map <string, int> cnt1;
  13.  
  14. int32_t main()
  15. {
  16. ios::sync_with_stdio(false);
  17. cin.tie(0); cout.tie(0);
  18. freopen("input.txt", "r", stdin);
  19. //freopen("output.txt", "w", stdout);
  20. cin >> n >> m;
  21. for (int i = 0; i < n; i++)
  22. {
  23. string s;
  24. cin >> s;
  25. int j = 0;
  26. while (s [j] != '.' && j < s.size())
  27. j++;
  28. pss p;
  29. if (j != s.size())
  30. {
  31. p.first = s.substr(0, j);
  32. p.second = s.substr(j, s.size());
  33. }
  34. else
  35. {
  36. p.first = s;
  37. p.second = "";
  38. }
  39. cnt [p.first] ++;
  40. cnt1 [p.second] ++;
  41. v.push_back(p);
  42. }
  43. if (cnt1 [""] > 0 && cnt1 ["."] > 0)
  44. {
  45. cnt1 [""] += cnt1 ["."];
  46. cnt1 ["."] = cnt1 [""];
  47. }
  48. map <string, bool> m1;
  49. map <string, bool> m2;
  50. m++;
  51. while (m > 1)
  52. {
  53. for (int i = 0; i < v.size(); i++)
  54. {
  55. if (cnt1 [v [i].second] == 1 && m1 [v [i].first] == 0 && m2 [v [i].second] == 0 && cnt [v [i].first] > 0)
  56. {
  57. cnt [v [i].first] --;
  58. if (v [i].second == "" || v [i].second == ".")
  59. {
  60. cnt1 [""]--;
  61. cnt1 ["."]--;
  62. }
  63. else
  64. cnt1 [v [i].second] --;
  65. m2 [v [i].second] = 1;
  66. continue;
  67. }
  68. }
  69. m--;
  70. if (m > 1)
  71. {
  72. for (int i = 0; i < v.size(); i++)
  73. {
  74. if (cnt [v [i].first] == 1 && m1 [v [i].first] == 0 && m2 [v [i].second] == 0 && cnt1 [v [i].second] > 0)
  75. {
  76. cnt [v [i].first] --;
  77. if (v [i].second == "" || v [i].second == ".")
  78. {
  79. cnt1 [""]--;
  80. cnt1 ["."]--;
  81. }
  82. else
  83. cnt1 [v [i].second] --;
  84. m1 [v [i].first] = 1;
  85. continue;
  86. }
  87. }
  88. }
  89. m1.clear();
  90. m2.clear();
  91. m--;
  92. }
  93. vector <string> ans;
  94. for (int i = 0; i < v.size(); i++)
  95. {
  96. if (cnt [v [i].first ] > 0 && cnt1 [v [i].second ] > 0)
  97. ans.push_back(v [i].first + v [i].second);
  98. }
  99. cout << ans.size() << "\n";
  100. for (int i = 0;i < ans.size(); i++)
  101. cout << ans [i] << "\n";
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement