【習題 6-9 UVA - 127】"Accordian" Patience
阿新 • • 發佈:2017-11-25
def esp ont ini size cor pan pen net
【鏈接】 我是鏈接,點我呀:)
【題意】
在這裏輸入題意
【題解】
鏈表模擬即可。
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;
}
【習題 6-9 UVA - 127】"Accordian" Patience