QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#253751#7697. Impartial Stringsucup-team004WA 52ms37736kbC++204.0kb2023-11-17 13:56:592023-11-17 13:56:59

Judging History

你现在查看的是最新测评结果

  • [2023-11-17 13:56:59]
  • 评测
  • 测评结果:WA
  • 用时:52ms
  • 内存:37736kb
  • [2023-11-17 13:56:59]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

std::vector<int> kmp(std::string s) {
    int n = s.size();
    std::vector<int> f(n + 1);
    for (int i = 1, j = 0; i < n; i++) {
        while (j && s[i] != s[j]) {
            j = f[j];
        }
        j += (s[i] == s[j]);
        f[i + 1] = j;
    }
    return f;
}
struct SCC {
    int n;
    std::vector<std::vector<int>> adj;
    std::vector<int> stk;
    std::vector<int> dfn, low, bel;
    int cur, cnt;
    
    SCC() {}
    SCC(int n) {
        init(n);
    }
    
    void init(int n) {
        this->n = n;
        adj.assign(n, {});
        dfn.assign(n, -1);
        low.resize(n);
        bel.assign(n, -1);
        stk.clear();
        cur = cnt = 0;
    }
    
    void addEdge(int u, int v) {
        adj[u].push_back(v);
    }
    
    void dfs(int x) {
        dfn[x] = low[x] = cur++;
        stk.push_back(x);
        
        for (auto y : adj[x]) {
            if (dfn[y] == -1) {
                dfs(y);
                low[x] = std::min(low[x], low[y]);
            } else if (bel[y] == -1) {
                low[x] = std::min(low[x], dfn[y]);
            }
        }
        
        if (dfn[x] == low[x]) {
            int y;
            do {
                y = stk.back();
                bel[y] = cnt;
                stk.pop_back();
            } while (y != x);
            cnt++;
        }
    }
    
    std::vector<int> work() {
        for (int i = 0; i < n; i++) {
            if (dfn[i] == -1) {
                dfs(i);
            }
        }
        return bel;
    }
};

void solve() {
    std::string A, S, T;
    std::cin >> A >> S >> T;
    
    int n = S.size(), m = T.size();
    
    auto fs = kmp(S), ft = kmp(T);
    
    std::vector<std::array<int, 26>> gs(n), gt(m);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 26; j++) {
            int x = i;
            while (x && S[x] != 'a' + j) {
                x = fs[x];
            }
            x += (S[x] == 'a' + j);
            gs[i][j] = x;
        }
    }
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < 26; j++) {
            int x = i;
            while (x && T[x] != 'a' + j) {
                x = ft[x];
            }
            x += (T[x] == 'a' + j);
            gt[i][j] = x;
        }
    }
    
    std::vector<std::vector<std::pair<int, int>>> adj(n * m);
    
    SCC g(n * m);
    
    for (int u = 0; u < n * m; u++) {
        int x = u / m, y = u % m;
        for (auto c : A) {
            int i = c - 'a';
            int nx = gs[x][i];
            int ny = gt[y][i];
            int w = 0;
            if (nx == n) {
                nx = fs[n];
                w += 1;
            }
            if (ny == m) {
                ny = ft[m];
                w -= 1;
            }
            int v = nx * m + ny;
            adj[u].emplace_back(v, w);
            g.addEdge(u, v);
        }
    }
    auto bel = g.work();
    
    std::queue<int> q;
    std::vector<int> f(n * m);
    std::vector<int> vis(n * m), visb(n * m);
    vis[0] = 1;
    visb[bel[0]] = 1;
    q.push(0);
    
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        int x = u / m, y = u % m;
        for (auto [v, w] : adj[u]) {
            if (bel[u] == bel[v]) {
                if (!vis[v]) {
                    f[v] = f[u] + w;
                    q.push(v);
                    vis[v] = 1;
                } else if (f[v] != f[u] + w) {
                    std::cout << 0 << "\n";
                    return;
                }
            } else if (!visb[bel[v]]) {
                visb[bel[v]] = 1;
                vis[v] = 1;
                q.push(v);
            }
        }
    }
    std::cout << 1 << "\n";
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int K;
    std::cin >> K;
    
    while (K--) {
        solve();
    }
    
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3536kb

input:

3
ab ab ba
abc ab ba
cz cczz zzcc

output:

1
0
0

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 52ms
memory: 37736kb

input:

7
d d d
d dd d
d d dd
z zzzzzzzzzzzz zzz
a aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

1
1
1
1
1
1
1

result:

ok 7 lines

Test #3:

score: -100
Wrong Answer
time: 1ms
memory: 3536kb

input:

10
ab aaaaaabbabbbbb bbbbba
ab aaaaaabbabbbbb baaaaaa
ab aaaaaabbabbbbb bbbba
ab aaaaaabbabbbbb bbba
ab aaaaaabbabbbbb baaa
ab aaaaaabbabbbbb baaaaa
ab aaaaaabbabbbbb baa
ab aaaaaabbabbbbb baaaa
ab aaaaaabbabbbbb baaaaaaa
ab aaaaaabbabbbbb bbbbbba

output:

0
0
0
0
0
0
0
0
0
0

result:

wrong answer 1st lines differ - expected: '1', found: '0'