QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#445823 | #8713. 不要玩弄字符串 | Andyqian7 | TL | 32ms | 451056kb | C++14 | 5.0kb | 2024-06-16 15:12:57 | 2024-06-16 15:12:58 |
Judging History
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];
inline int val(int i, int j)
{
int m, tmp, res = -1e9;
m = match[j << 1] & i;
tmp = -dp[i & ~m][j << 1 & 127] + sum[m];
dp[i][j] = max(dp[i][j], tmp);
if (tmp < 1e7)
res = max(tmp, res);
m = match[j << 1 | 1] & i;
tmp = -dp[i & ~m][(j << 1 | 1) & 127] + sum[m];
dp[i][j] = max(dp[i][j], tmp);
if (tmp < 1e7)
res = max(tmp, res);
return res;
}
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;
auto func = [=](int x, int y)
{
return val(i, x) < val(i, y);
};
priority_queue<int, vector<int>, decltype(func)> undet(func);
for (int j = 0; j < 128; j++)
{
int m, tmp;
if ((match[j << 1] & i) && (match[j << 1 | 1] & i))
{
m = match[j << 1] & i;
tmp = -dp[i & ~m][j << 1 & 127] + sum[m];
dp[i][j] = max(dp[i][j], tmp);
m = match[j << 1 | 1] & i;
tmp = -dp[i & ~m][(j << 1 | 1) & 127] + sum[m];
dp[i][j] = max(dp[i][j], tmp);
}
else
{
undet.push(j);
}
}
while (undet.size())
{
int j = undet.top();
dp[i][j] = max(val(i, j),0);
undet.pop();
}
}
}
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: 32ms
memory: 451056kb
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
Time Limit Exceeded
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...