Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define int int64_t
- //GCC optimize ("O3");
- using namespace std;
- typedef long long ll;
- typedef pair <string, string> pss;
- typedef vector <pss> vpss;
- int n, m;
- vpss v;
- map <string, int> cnt;
- map <string, int> cnt1;
- int32_t main()
- {
- ios::sync_with_stdio(false);
- cin.tie(0); cout.tie(0);
- freopen("input.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- cin >> n >> m;
- for (int i = 0; i < n; i++)
- {
- string s;
- cin >> s;
- int j = 0;
- while (s [j] != '.' && j < s.size())
- j++;
- pss p;
- if (j != s.size())
- {
- p.first = s.substr(0, j);
- p.second = s.substr(j, s.size());
- }
- else
- {
- p.first = s;
- p.second = "";
- }
- cnt [p.first] ++;
- cnt1 [p.second] ++;
- v.push_back(p);
- }
- if (cnt1 [""] > 0 && cnt1 ["."] > 0)
- {
- cnt1 [""] += cnt1 ["."];
- cnt1 ["."] = cnt1 [""];
- }
- map <string, bool> m1;
- map <string, bool> m2;
- m++;
- while (m > 1)
- {
- for (int i = 0; i < v.size(); i++)
- {
- if (cnt1 [v [i].second] == 1 && m1 [v [i].first] == 0 && m2 [v [i].second] == 0 && cnt [v [i].first] > 0)
- {
- cnt [v [i].first] --;
- if (v [i].second == "" || v [i].second == ".")
- {
- cnt1 [""]--;
- cnt1 ["."]--;
- }
- else
- cnt1 [v [i].second] --;
- m2 [v [i].second] = 1;
- continue;
- }
- }
- m--;
- if (m > 1)
- {
- for (int i = 0; i < v.size(); i++)
- {
- if (cnt [v [i].first] == 1 && m1 [v [i].first] == 0 && m2 [v [i].second] == 0 && cnt1 [v [i].second] > 0)
- {
- cnt [v [i].first] --;
- if (v [i].second == "" || v [i].second == ".")
- {
- cnt1 [""]--;
- cnt1 ["."]--;
- }
- else
- cnt1 [v [i].second] --;
- m1 [v [i].first] = 1;
- continue;
- }
- }
- }
- m1.clear();
- m2.clear();
- m--;
- }
- vector <string> ans;
- for (int i = 0; i < v.size(); i++)
- {
- if (cnt [v [i].first ] > 0 && cnt1 [v [i].second ] > 0)
- ans.push_back(v [i].first + v [i].second);
- }
- cout << ans.size() << "\n";
- for (int i = 0;i < ans.size(); i++)
- cout << ans [i] << "\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement