b211困難

95 臺中區資訊學科能力競賽(2006)|字串表示法判定

先把字元、串接、重複n次與Kleene星號解析成語法樹,每個n都使用同一候選值。

解題思路

先把字元、串接、重複n次與Kleene星號解析成語法樹,每個n都使用同一候選值。

對長L輸入,節點關係R[i]記錄從位置i匹配該節點後可到的所有位置。串接作布林關係合成,n次重複用快速冪,星號作可達閉包(含0次)。

若有解,非空n次重複限制n≤L;若所有被n修飾的節點皆可空,超過L的次數可刪空匹配或用空匹配補足,所以只須測1至max(1,L)。根節點從0可到L即接受。

設表示法P節點、字串長L,位元矩陣合成最壞 O(L³)次位元合併,總保守 O(P L⁴ log L),空間 O(P L²)位元加結構;未宣稱線性正規式匹配。

Step-by-step animation

教學小例:固定共同n的布林位置關係

回報動畫錯誤
步驟 1 / 3

表示法anbn、輸入aabb

測n=1只接受ab

測n=1只接受ab
步驟
1

Reference solution

回報程式錯誤

完整程式

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using boost::multiprecision::cpp_int;
struct Node {
    char kind;
    vector<int> ch;
};

int main() {
    string expr;
    getline(cin, expr);
    vector<Node> nodes;
    vector<vector<int>> stack(1);
    for (char c : expr) {
        if (c == 'a' || c == 'b') {
            nodes.push_back({c, {}});
            stack.back().push_back(nodes.size() - 1);
        } else if (c == '(')
            stack.push_back({});
        else if (c == ')') {
            auto ch = stack.back();
            stack.pop_back();
            nodes.push_back({'s', ch});
            stack.back().push_back(nodes.size() - 1);
        } else if (c == 'n' || c == '*') {
            int x = stack.back().back();
            nodes.push_back({c, {x}});
            stack.back().back() = nodes.size() - 1;
        }
    }
    nodes.push_back({'s', stack[0]});
    int root = nodes.size() - 1;
    for (int test = 1; test <= 4; test++) {
        string s;
        getline(cin, s);
        int L = s.size();
        bool accepted = false;
        using Rel = vector<cpp_int>;
        Rel identity(L + 1);
        for (int i = 0; i <= L; i++)
            identity[i] = cpp_int(1) << i;
        auto compose = [&](const Rel &a, const Rel &b) {
            Rel out(L + 1);
            for (int i = 0; i <= L; i++) {
                cpp_int bits = a[i];
                while (bits != 0) {
                    unsigned j = boost::multiprecision::lsb(bits);
                    bits &= bits - 1;
                    out[i] |= b[j];
                }
            }
            return out;
        };
        for (int n = 1; n <= max(1, L) && !accepted; n++) {
            vector<Rel> dp;
            for (auto node : nodes) {
                Rel r(L + 1);
                if (node.kind == 'a' || node.kind == 'b') {
                    for (int i = 0; i < L; i++)
                        if (s[i] == node.kind)
                            r[i] = cpp_int(1) << (i + 1);
                } else if (node.kind == 's') {
                    r = identity;
                    for (int ch : node.ch)
                        r = compose(r, dp[ch]);
                } else if (node.kind == 'n') {
                    r = identity;
                    Rel b = dp[node.ch[0]];
                    int e = n;
                    while (e) {
                        if (e & 1)
                            r = compose(r, b);
                        e /= 2;
                        if (e)
                            b = compose(b, b);
                    }
                } else {
                    auto &b = dp[node.ch[0]];
                    for (int i = L; i >= 0; i--) {
                        r[i] = cpp_int(1) << i;
                        cpp_int bits = b[i];
                        while (bits != 0) {
                            int j = boost::multiprecision::lsb(bits);
                            bits &= bits - 1;
                            if (j > i)
                                r[i] |= r[j];
                        }
                    }
                }
                dp.push_back(move(r));
            }
            accepted = boost::multiprecision::bit_test(dp[root][0], L);
        }
        cout << test << ". The input string is " << (accepted ? "accepted" : "rejected")
             << '\n';
    }
}

先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。

更多