Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- bool check(string a, string b) {
- return a[0] == b[0] || a[1] == b[1];
- }
- int main()
- {
- int n;
- cin >> n;
- vector<stack<string>> v(n);
- for (int i = 0; i < n; ++i) {
- string s;
- cin >> s;
- v[i].push(s);
- }
- for (int i = 0; i < v.size(); ++i) {
- while (i < v.size() && v[i].empty())
- v.erase(v.begin()+i);
- if (i >= v.size()) break;
- if (i-3 >= 0 && check(v[i-3].top(), v[i].top())) {
- v[i-3].push(v[i].top());
- v[i].pop();
- i = i-4;
- } else if (i-1 >= 0 && check(v[i-1].top(), v[i].top())) {
- v[i-1].push(v[i].top());
- v[i].pop();
- i = i-2;
- }
- }
- printf("%d\n", v.size());
- for (int i = 0; i < v.size(); ++i)
- printf("%d ", v[i].size());
- return 0;
- }
Add Comment
Please, Sign In to add comment