QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#446346#8713. 不要玩弄字符串Andyqian7WA 2301ms209512kbC++145.1kb2024-06-17 08:40:112024-06-17 08:40:12

Judging History

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

  • [2024-06-17 08:40:12]
  • 评测
  • 测评结果:WA
  • 用时:2301ms
  • 内存:209512kb
  • [2024-06-17 08:40:11]
  • 提交

answer

#include <bits/stdc++.h>
#define ppc __builtin_popcount
#define MAXK 18
#define pii pair<int, int>
using namespace std;
int k, q, v[20];
string t[20];
int nxt[200][2], fail[200], tag[200], dp[200], top = 3, sum[1 << MAXK], dp2[1 << MAXK][200], vis[200], post[200][2];
vector<int> sonfail[200], pre[200];
void dfs(int s)
{
    if (nxt[s][0])
        post[s][0] = nxt[s][0];
    if (nxt[s][1])
        post[s][1] = nxt[s][1];
    if (tag[s] >= 0)
        dp[s] |= 1 << tag[s];
    for (int son : sonfail[s])
    {
        dp[son] |= dp[s];
        post[son][0] = post[s][0], post[son][1] = post[s][1];
        dfs(son);
    }
}
void build()
{
    memset(tag, 255, sizeof tag);
    nxt[1][0] = 2, nxt[1][1] = 3;
    fail[1] = fail[2] = fail[3] = 1;
    for (int i = 0; i < k; i++)
    {
        int cur = 1;
        for (char c : t[i])
        {
            c -= 48;
            if (!nxt[cur][c])
            {
                nxt[cur][c] = ++top;
            }
            cur = nxt[cur][c];
        }
        tag[cur] = i;
    }
    queue<int> Q;
    Q.push(1);
    while (Q.size())
    {
        int cur = Q.front();
        Q.pop();
        for (int i = 0; i < 2; i++)
        {
            if (!nxt[cur][i])
                continue;
            int tmp = fail[cur];
            while (!nxt[tmp][i])
                tmp = fail[tmp];
            if (nxt[tmp][i] != nxt[cur][i])
                fail[nxt[cur][i]] = nxt[tmp][i];
            else
                fail[nxt[cur][i]] = 1;
            Q.push(nxt[cur][i]);
        }
    }
    for (int i = 2; i <= top; i++)
    {
        sonfail[fail[i]].push_back(i);
    }
    dfs(1);
    for (int i = 1; i <= top; i++)
    {
        pre[post[i][0]].push_back(i), pre[post[i][1]].push_back(i);
    }
}
int val(int i, int j)
{
    int ma = -1e9;
    if (-dp2[i][post[j][0]] < 1e7)
    {
        ma = max(ma, -dp2[i][post[j][0]]);
    }
    if (-dp2[i][post[j][1]] < 1e7)
    {
        ma = max(ma, -dp2[i][post[j][1]]);
    }
    return ma;
}
inline bool fixed(int i, int j)
{
    return -dp2[i][post[j][0]] < 1e7 && -dp2[i][post[j][1]] < 1e7;
}
int main()
{
    cin >> k;
    for (int i = 0; i < k; i++)
    {
        cin >> t[i] >> v[i];
    }
    build();
    for (int i = 0; i < 1 << k; i++)
    {
        for (int j = 0; j < k; j++)
        {
            if (i >> j & 1)
            {
                sum[i] += v[j];
            }
        }
    }
    for (int i = 1; i < 1 << MAXK; i++)
    {
        for (int j = 1; j <= top; j++)
        {
            dp2[i][j] = -1e9;
        }
    }
    for (int sz = 1; sz <= k; sz++)
    {
        for (int i = 0; i < 1 << k; i++)
        {
            if (ppc(i) != sz)
            {
                continue;
            }
            memset(vis, 0, (top + 1) * sizeof(int));
            priority_queue<pii> und;
            queue<int> Q;
            for (int j = 1; j <= top; j++)
            {
                if (dp[j] & i)
                {
                    dp2[i][j] = max(dp2[i][j], dp2[i & ~dp[j]][j] - sum[dp[j] & i]);
                    vis[j] = 1;
                }
                else if (fixed(i, j))
                {
                    dp2[i][j] = val(i, j);
                    vis[j] = 1;
                }
            }
            for (int j = 1; j <= top; j++)
            {
                if (!(dp[j] & i) && !vis[j])
                {
                    und.push({val(i, j), j});
                }
            }
            while (und.size())
            {
                auto [v, t] = und.top();
                und.pop();
                if (vis[t])
                    continue;
                vis[t] = 1;
                dp2[i][t] = max(v, 0);
                for (int p : pre[t])
                {
                    if (vis[p])
                        continue;
                    if (fixed(i, p))
                        Q.push(p);
                    else
                        und.push({val(i, p), p});
                }
                while (Q.size())
                {
                    int t = Q.front();
                    Q.pop();
                    if (vis[t])
                        continue;
                    vis[t] = 1;
                    dp2[i][t] = val(i, t);
                    for (int p : pre[t])
                    {
                        if (vis[p])
                            continue;
                        if (fixed(i, p))
                            Q.push(p);
                        else
                            und.push({val(i, p), p});
                    }
                }
            }
        }
    }
    cin >> q;
    while (q--)
    {
        string s;
        cin >> s;
        int cur = 1, st = (1 << k) - 1;
        for (char c : s)
        {
            c -= 48;
            while (!nxt[cur][c])
            {
                cur = fail[cur];
            }
            if (nxt[cur][c])
            {
                cur = nxt[cur][c];
            }
            st &= ~dp[cur];
        }
        cout << dp2[st][cur] << endl;
    }
}

详细

Test #1:

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

input:

3
11 1
0 2
000 3
2
0
1

output:

-1
3

result:

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

Test #2:

score: -100
Wrong Answer
time: 2301ms
memory: 209512kb

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
0
0
0
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
0
0
0
-77190
-77190
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
89827
89827
0
84183
77190
77...

result:

wrong answer 48th numbers differ - expected: '26277', found: '0'