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

Posted Visitor

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了习题 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;
}

以上是关于习题 6-9 UVA - 127"Accordian" Patience的主要内容,如果未能解决你的问题,请参考以下文章

UVa127 "Accordian" Patience (模拟)

"Accordian" Patience (UVa 127) 双向链表 栈

习题 6-1 UVA-673Parentheses Balance

6-9 天平 uva839

习题3-4 周期串 UVa455

UVa 12100 Printer Queue (习题 5-7)