QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#445740#8713. 不要玩弄字符串Andyqian7WA 1248ms451332kbC++144.9kb2024-06-16 11:38:542024-06-16 11:38:56

Judging History

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

  • [2024-06-16 11:38:56]
  • 评测
  • 测评结果:WA
  • 用时:1248ms
  • 内存:451332kb
  • [2024-06-16 11:38:54]
  • 提交

answer

#include <bits/stdc++.h>
#define ppc __builtin_popcount
#define ctz __builtin_ctz
#define lowbit(x) x & -x
#define MAXK 18
using namespace std;
string t[MAXK];
int k, q, mp[9][256], v[MAXK], dp[1 << MAXK][128], match[256], match2[9][256], sum[1 << MAXK];
int *dp2[8][1 << MAXK];
int main()
{
    cin >> k;
    for (int i = 0; i < k; i++)
    {
        cin >> t[i] >> v[i];
    }
    memset(mp, 255, sizeof mp);
    for (int l = 0; l <= 7; l++)
    {
        for (int i = 0; i < 1<<MAXK; i++)
        {
            dp2[l][i] = new int[1 << l];
        }
    }

    for (int l = 0; l < 7; l++)
    {

        for (int i = 0; i < 1 << MAXK; i++)
        {
            for (int j = 0; j < 1<<l; j++)
            {
                dp2[l][i][j] = -1e9;
            }
        }
    }

    for (int i = 0; i < 1 << MAXK; i++)
    {
        for (int j = 0; j < 128; j++)
        {
            dp[i][j] = -1e9;
        }
    }

    for (int i = 0; i < k; i++)
    {
        int tmp = 0;
        for (char c : t[i])
        {
            tmp = tmp << 1 | (c - 48);
        }
        mp[t[i].length()][tmp] = i;
    }
    for (int x = 0; x < 256; x++)
    {
        int res = 0;
        for (int i = 1; i <= 8; i++)
        {
            if (mp[i][x & ((1 << i) - 1)] >= 0)
            {
                res |= 1 << mp[i][x & ((1 << i) - 1)];
            }
        }
        match[x] = res;
    }
    for (int l = 0; l <= 8; l++)
        for (int x = 0; x < 1 << l; x++)
        {
            int res = 0;
            for (int i = 1; i <= l; i++)
            {
                if (mp[i][x & ((1 << i) - 1)] >= 0)
                {
                    res |= 1 << mp[i][x & ((1 << i) - 1)];
                }
            }
            match2[l][x] = res;
        }
    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 j = 0; j < 128; j++)
    {
        dp[0][j] = 0;
    }
    for (int l = 1; l <= k; l++)
    {
        for (int i = 0; i < 1 << k; i++)
        {
            if (ppc(i) != l)
                continue;
            for (int j = 0; j < 128; j++)
            {
                int m, tmp;
                m = match[j << 1] & i;
                if (m)
                {
                    tmp = -dp[i & ~m][j << 1 & 127] + sum[m];
                    dp[i][j] = max(dp[i][j], tmp);
                }
                m = match[j << 1 | 1] & i;
                if (m)
                {
                    tmp = -dp[i & ~m][(j << 1 | 1) & 127] + sum[m];
                    dp[i][j] = max(dp[i][j], tmp);
                }
            }
            for (int k = 0; k < 7; k++)
            {
                for (int j = 0; j < 128; j++)
                {
                    int m, tmp;
                    m = match[j << 1] & i;
                    if (!m)
                    {
                        tmp = -dp[i & ~m][j << 1 & 127] + sum[m];
                        if (tmp > 1e7)
                            tmp = 0;
                        dp[i][j] = max(dp[i][j], tmp);
                    }
                    m = match[j << 1 | 1] & i;
                    if (!m)
                    {
                        tmp = -dp[i & ~m][(j << 1 | 1) & 127] + sum[m];
                        if (tmp > 1e7)
                            tmp = 0;
                        dp[i][j] = max(dp[i][j], tmp);
                    }
                }
            }
        }
    }

    for (int i = 0; i < 1 << k; i++)
    {
        for (int j = 0; j < 128; j++)
        {
            dp2[7][i][j] = dp[i][j];
        }
    }

    for (int l = 6; l; l--)
    {
        for (int i = 0; i < 1 << k; i++)
        {
            for (int j = 0; j < 1 << l; j++)
            {
                int m, tmp;
                m = match2[l + 1][j << 1] & i;
                tmp = -dp2[l + 1][i & ~m][j << 1] + sum[m];
                dp2[l][i][j] = max(dp2[l][i][j], tmp);
                m = match2[l + 1][j << 1 | 1] & i;
                tmp = -dp2[l + 1][i & ~m][j << 1 | 1] + sum[m];
                dp2[l][i][j] = max(dp2[l][i][j], tmp);
            }
        }
    }
    cin >> q;
    while (q--)
    {
        string S;
        cin >> S;
        int tmp = 0;
        for (char c : S)
        {
            tmp = tmp << 1 | (c - 48);
        }
        int m = 0;
        for (int i = 0; i < S.length(); i++)
        {
            for (int j = 1; j <= min(8, (int)S.length() - i); j++)
            {
                if (mp[j][tmp >> i & ((1 << j) - 1)] >= 0)
                {
                    m |= 1 << mp[j][tmp >> i & ((1 << j) - 1)];
                }
            }
        }
        cout << dp2[min((int)S.length(), 7)][((1 << k) - 1) & ~m][tmp & 127] << endl;
    }
}
/*
3
11 1
0 2
000 3
2
0
1
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 59ms
memory: 450072kb

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: 1248ms
memory: 451332kb

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
84009
77190
0
0
0
0
0
0
0
0
0
77190
0
0
0
31681
174
-77190
0
0
0
0
0
0
0
53108
89827
84009
77190
77190
14949
0
18677
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
77190
0
0
0
0
0
0
31681
-53108
25412
174
-77190
-77190
0
0
0
0
0
0
0
0
0
0
0
0
0
0
53108
53108...

result:

wrong answer 25th numbers differ - expected: '84183', found: '84009'