QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#253754#7697. Impartial Stringsucup-team004AC ✓2043ms77600kbC++202.7kb2023-11-17 14:10:392023-11-17 14:10:39

Judging History

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

  • [2023-11-17 14:10:39]
  • 评测
  • 测评结果:AC
  • 用时:2043ms
  • 内存:77600kb
  • [2023-11-17 14:10:39]
  • 提交

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;
}

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);
    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);
        }
    }
    
    auto check = [&](int c) {
        std::queue<int> q;
        std::vector<int> f(n * m, 1E9);
        std::vector<int> inq(n * m);
        q.push(0);
        f[0] = 0;
        inq[0] = 1;
        int t = 0;
        while (!q.empty()) {
            int x = q.front();
            q.pop();
            inq[x] = 0;
            t += 1;
            if (t > (n + m) * (n + m)) {
                return true;
            }
            for (auto [y, w] : adj[x]) {
                w *= c;
                if (f[y] > f[x] + w) {
                    f[y] = f[x] + w;
                    if (!inq[y]) {
                        q.push(y);
                        inq[y] = 1;
                    }
                }
            }
        }
        return false;
    };
    
    if (check(1) && check(-1)) {
        std::cout << 0 << "\n";
    } else {
        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;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3600kb

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: 31ms
memory: 19004kb

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: 0
Accepted
time: 1ms
memory: 3612kb

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:

1
1
1
1
1
1
1
1
0
0

result:

ok 10 lines

Test #4:

score: 0
Accepted
time: 1831ms
memory: 19360kb

input:

50
az aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

result:

ok 50 lines

Test #5:

score: 0
Accepted
time: 1308ms
memory: 19572kb

input:

50
az aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1

result:

ok 50 lines

Test #6:

score: 0
Accepted
time: 1546ms
memory: 19364kb

input:

50
az zaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

result:

ok 50 lines

Test #7:

score: 0
Accepted
time: 1403ms
memory: 19528kb

input:

50
az azaaaaaaazaazzzzazzzaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

result:

ok 50 lines

Test #8:

score: 0
Accepted
time: 1239ms
memory: 19360kb

input:

50
az zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1

result:

ok 50 lines

Test #9:

score: 0
Accepted
time: 2043ms
memory: 19360kb

input:

50
az zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

result:

ok 50 lines

Test #10:

score: 0
Accepted
time: 1224ms
memory: 19272kb

input:

50
az aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1

result:

ok 50 lines

Test #11:

score: 0
Accepted
time: 1968ms
memory: 19280kb

input:

50
az aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

result:

ok 50 lines

Test #12:

score: 0
Accepted
time: 557ms
memory: 9076kb

input:

50
ab bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1

result:

ok 50 lines

Test #13:

score: 0
Accepted
time: 341ms
memory: 14836kb

input:

50
icp cppcpccpiccccccppiiiipicpcipicciciciccipcpcccpipcpppcipicipiipppipccppppiiippcpcpiciipcpipiipcccpiciiiicpipipcpcpicccpicppcciicippipcpicippcipppcppciiccicipccppccpccpipciipiippciippppicccciiicpciicpcppcipcippcppccipipcppcccppppciiccpippcipiipciccciccccccppipcccccicicpcppippciiccpiccppppccippc...

output:

0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

result:

ok 50 lines

Test #14:

score: 0
Accepted
time: 1125ms
memory: 67744kb

input:

50
oks kksksoskssksskssksoksoskkkoooskkskskkkkkkokssksokoskkksooskkkokkkksosssoksskokokoksooosksskkkosssosoooskssokkooossskssssosossksoookkss koskskkkksssskkoossosskkskoskokokooksokossssokookkokosssoosoosskksokskskokkokokosookksokksokokooskokokkkkookosokooosoosksksoskkoookoskksksoskskssskoskskkkkskk...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

result:

ok 50 lines

Test #15:

score: 0
Accepted
time: 1229ms
memory: 77600kb

input:

50
swxyrcjgp jjxypwcpgjxxcwccgwpsyxgjpwrgwpxwywwwxsxsyyygypwjrwpgcxgcgpgrrjwgjgpjsswjwpsjyxxyxscwcsxysywwyspgrspjsxypyyxsgyssswrcrpjgrygxwgjcyppysycpxyjjjgrgyxcwyywsrpyccrryprcjwgswrcjjgjxwyrpsgjpccrjwsjwxgjpsjssjycxxjprycxjxrpwgxcpjycwxyrgpppwwjjjcjrjxssxwyryxjyjjyjcjcsjsccpxxgscsspsryrjswpcwjgspsx...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1

result:

ok 50 lines

Test #16:

score: 0
Accepted
time: 0ms
memory: 3620kb

input:

1
az aaaaza azaaaa

output:

0

result:

ok single line: '0'

Test #17:

score: 0
Accepted
time: 835ms
memory: 19360kb

input:

50
ab aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1

result:

ok 50 lines