QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#418138#8713. 不要玩弄字符串light_ink_dotsWA 601ms158452kbC++143.2kb2024-05-23 11:10:112024-05-23 11:10:12

Judging History

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

  • [2024-05-23 11:10:12]
  • 评测
  • 测评结果:WA
  • 用时:601ms
  • 内存:158452kb
  • [2024-05-23 11:10:11]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

int main() {
    static const int maxk = 18, maxu = 150;
    int k;
    scanf("%d", &k);
    static int v[1 << maxk];
    static int nxt[maxu][2], id[maxu], cnt = 1;
    nxt[1][0] = ++cnt, nxt[1][1] = ++cnt;
    for (int i = 0; i < k; i++) {
        static char s[maxu];
        scanf("%s", s + 1), scanf("%d", v + (1 << i));
        int l = strlen(s + 1), u = 1;
        for (int p = 1; p <= l; p++) {
            int d = s[p] == '1';
            if (!nxt[u][d])
                nxt[u][d] = ++cnt;
            u = nxt[u][d];
        }
        id[u] ^= 1 << i;
    }
    queue<int> Q;
    static int fa[maxu], dep[maxu];
    for (int i : nxt[1]) fa[i] = dep[i] = 1, Q.push(i);
    while (!Q.empty()) {
        int u = Q.front();
        Q.pop();
        for (int d = 0; d < 2; d++)
            if (nxt[u][d])
                dep[nxt[u][d]] = dep[fa[nxt[u][d]] = nxt[fa[u]][d]] + 1, Q.push(nxt[u][d]);
            else
                nxt[u][d] = nxt[fa[u]][d];
    }
    static int c[maxu], ord[maxu];
    for (int i = 1; i <= cnt; i++) c[dep[i]]++;
    for (int i = 1; i <= cnt; i++) c[i] += c[i - 1];
    for (int i = 1; i <= cnt; i++) ord[c[dep[i]]--] = i;
    for (int i = 1; i <= cnt; i++) id[ord[i]] |= id[fa[ord[i]]];
    for (int s = 1; s < 1 << k; s++) v[s] = v[s ^ (s & -s)] + v[s & -s];
    static int dp[1 << maxk][maxu];
    for (int s = (1 << k) - 2; s >= 0; s--) {
        static int deg[maxu];
        static vector<int> G[maxu];
        memset(dp[s], 0xcf, sizeof(dp[s]));
        for (int u = 1; u <= cnt; u++) deg[u] = 0, G[u].clear();
        for (int u = 1; u <= cnt; u++) {
            int t0 = nxt[u][0], t1 = nxt[u][1];
            if ((id[t0] | s) != s)
                dp[s][u] = max(dp[s][u], v[id[t0]] - dp[s | id[t0]][t0]);
            else
                G[t0].push_back(u), deg[u]++;
            if ((id[t1] | s) != s)
                dp[s][u] = max(dp[s][u], v[id[t1]] - dp[s | id[t1]][t1]);
            else
                G[t1].push_back(u), deg[u]++;
        }
        struct node {
            int u, d;
            inline bool operator<(const node rhs) const { return abs(d) < abs(rhs.d); }
        };
        priority_queue<node> Q;
        static bool vis[maxu];
        memset(vis, 0, sizeof(vis));
        for (int u = 1; u <= cnt; u++)
            if (!deg[u] || dp[s][u] > 0)
                Q.push({ u, dp[s][u] });
        while (!Q.empty()) {
            int u = Q.top().u, d = Q.top().d;
            Q.pop();
            if (dp[s][u] != d || vis[u])
                continue;
            vis[u] = true;
            for (int i : G[u]) {
                dp[s][i] = max(dp[s][i], -dp[s][u]);
                if (!(--deg[i]) || dp[s][i] > 0)
                    Q.push({ i, dp[s][i] });
            }
        }
        for (int u = 1; u <= cnt; u++)
            if (deg[u])
                dp[s][u] = max(dp[s][u], 0);
    }
    int q;
    scanf("%d", &q);
    while (q--) {
        static char s[maxu];
        scanf("%s", s + 1);
        int l = strlen(s + 1), S = 0, u = 1;
        for (int i = 1; i <= l; i++) S |= id[u = nxt[u][s[i] == '1']];
        printf("%d\n", dp[S][u]);
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
11 1
0 2
000 3
2
0
1

output:

-1
3

result:

ok 2 number(s): "-1 3"

Test #2:

score: 0
Accepted
time: 327ms
memory: 158412kb

input:

18
111101 -31301
1101101 53902
101001 77190
1000110 -26277
01010 84183
0111001 -89769
100100 31681
00101 -33612
00101000 -25238
00111 -93542
0001101 45298
01100010 63521
11001001 84789
001011 89827
11010101 -12647
01011100 18677
010100 174
10011111 -73155
524286
0
1
00
10
01
11
000
100
010
110
001
1...

output:

0
0
0
0
0
0
0
0
0
0
0
77190
0
0
0
0
0
0
0
-77190
0
0
0
0
84183
77190
0
0
0
0
0
0
0
0
0
77190
0
0
0
31681
0
-77190
0
0
0
0
0
26277
0
53108
89827
84183
77190
77190
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
77190
77190
0
0
0
0
0
0
31681
-53108
0
0
-77190
-77190
0
0
0
0
0
0
0
0
0
0
26277
26277
0
0
53108
53108...

result:

ok 524286 numbers

Test #3:

score: -100
Wrong Answer
time: 601ms
memory: 158452kb

input:

18
01 3
000 13
100 9
011 1
0101 6
0011 16
0100 12
0111 10
00100 15
1110 2
011111 18
100100 6
1010001 8
10101100 3
1001000 9
00100001 12
001100 8
10111111 2
524286
0
1
00
10
01
11
000
100
010
110
001
101
011
111
0000
1000
0100
1100
0010
1010
0110
1110
0001
1001
0101
1101
0011
1011
0111
1111
00000
100...

output:

10
0
10
10
-7
0
3
13
16
10
9
-7
8
0
3
0
13
13
17
16
8
12
0
9
-7
-7
8
8
2
0
3
0
0
0
19
11
13
13
30
16
10
16
9
8
0
12
0
9
9
9
-7
-7
-8
-9
17
8
8
8
2
2
16
0
3
0
0
0
-6
2
0
0
6
28
11
11
8
13
13
13
30
30
14
16
10
10
17
18
17
9
8
8
0
0
0
12
0
9
9
9
9
9
9
7
-16
-7
-7
-7
-8
-8
0
-9
17
8
8
8
8
8
8
10
-7
2
2
...

result:

wrong answer 9th numbers differ - expected: '13', found: '16'