QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#445735#8713. 不要玩弄字符串Andyqian7ML 0ms0kbC++144.7kb2024-06-16 11:30:212024-06-16 11:30:21

Judging History

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

  • [2024-06-16 11:30:21]
  • 评测
  • 测评结果:ML
  • 用时:0ms
  • 内存:0kb
  • [2024-06-16 11:30:21]
  • 提交

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], dp2[8][1 << MAXK][128], match[256], match2[9][256], sum[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++)
    {
        for (int j = 0; j < 128; 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: 0
Memory Limit Exceeded

input:

3
11 1
0 2
000 3
2
0
1

output:

-1
3

result: