QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#446445#8713. 不要玩弄字符串Andyqian7WA 7ms208684kbC++145.7kb2024-06-17 10:47:162024-06-17 10:47:16

Judging History

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

  • [2024-06-17 10:47:16]
  • 评测
  • 测评结果:WA
  • 用时:7ms
  • 内存:208684kb
  • [2024-06-17 10:47:16]
  • 提交

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];
        dp[s] |= 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()
{
    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] |= 1 << 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()
{
    freopen("out.txt", "r", stdin);
    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;
                }
            }
            for (int j = 1; j <= top; j++)
            {
                if (!(dp[j] & i))
                {
                    if (fixed(i, j))
                        Q.push(j);
                    else
                        und.push({val(i, j), j});
                }
            }
            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});
                }
            }
            while (und.size())
            {
                auto [v, t] = und.top();
                und.pop();
                if (vis[t] || v <= 0)
                    continue;
                vis[t] = 1;
                dp2[i][t] = v;
                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});
                    }
                }
            }
            for (int j = 1; j <= top; j++)
            {
                if (dp2[i][j] < -1e7)
                {
                    dp2[i][j] = 0;
                }
            }
        }
    }
    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: 0
Wrong Answer
time: 7ms
memory: 208684kb

input:

3
11 1
0 2
000 3
2
0
1

output:


result:

wrong answer Answer contains longer sequence [length = 2], but output contains 0 elements