【习题 6-9 UVA - 127】"Accordian" Patience

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

链表模拟即可。 1pile不能加s...

【代码】

#include <bits/stdc++.h>
using namespace std;

const int N = 60;
string s[N];

int l[N], r[N];
vector <string> v[N];

int main() {
	#ifdef LOCAL_DEFINE
		freopen("F:\\c++source\\rush_in.txt", "r", stdin);
	#endif
	ios::sync_with_stdio(0), cin.tie(0);
	while (cin >> s[1] && s[1] != "#") {
		for (int i = 2; i <= 52; i++) cin >> s[i];

		for (int i = 1; i <= 52; i++) v[i].clear();
		for (int i = 1; i <= 52; i++) v[i].push_back(s[i]);

		for (int i = 0; i <= 52; i++) l[i] = i - 1, r[i] = i + 1;

		bool ok = true;
		while (ok) {
			ok = false;
			for (int i = r[0]; i != 53; i = r[i]) {
				int x1 = -1, x2 = -1, x3 = l[i];
				if (x3 >= 0) x2 = l[x3];
				if (x2 >= 0) x1 = l[x2];

				if (x1 >= 1 && x1 <= 52 && (v[x1].back()[0] == v[i].back()[0] || v[x1].back()[1] == v[i].back()[1])) {
					v[x1].push_back(v[i].back());
					v[i].pop_back();
					ok = true;
				}else
					if (x3 >= 1 && x3 <= 52 && (v[x3].back()[0] == v[i].back()[0] || v[x3].back()[1] == v[i].back()[1])) {
						v[x3].push_back(v[i].back());
						v[i].pop_back();
						ok = true;
					}

				if (v[i].empty()) {
					int ll = l[i], rr = r[i];
					r[ll] = rr;
					l[rr] = ll;
				}
				if (ok) break;
			}
		}
		vector <int> ans;
		ans.clear();
		for (int i = r[0]; i != 53; i = r[i]) {
			ans.push_back(v[i].size());
		}
		cout << (int)ans.size() << " pile";
		if ((int)ans.size()!=1) cout <<"s";
		cout<<" remaining:";
		for (int i = 0; i <(int)ans.size(); i++)
			cout << ' ' << ans[i];
		cout << endl;
	}
	return 0;
}
posted @ 2017-11-25 21:16  AWCXV  阅读(131)  评论(0编辑  收藏  举报