QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#446390 | #8713. 不要玩弄字符串 | Andyqian7 | WA | 2403ms | 209716kb | C++14 | 5.6kb | 2024-06-17 09:34:05 | 2024-06-17 09:34:05 |
Judging History
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;
}
}
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])
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: 12ms
memory: 208516kb
input:
3 11 1 0 2 000 3 2 0 1
output:
-1 3
result:
ok 2 number(s): "-1 3"
Test #2:
score: 0
Accepted
time: 2403ms
memory: 209432kb
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 26277 0 53108 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 -53108 0 0 -77190 -77190 0 0 0 0 0 0 0 0 0 0 26277 26277 0 0 53108 53108...
result:
ok 524286 numbers
Test #3:
score: 0
Accepted
time: 1047ms
memory: 209716kb
input:
18 01 3 000 13 100 9 011 1 0101 6 0011 16 0100 12 0111 10 00100 15 1110 2 011111 18 100100 6 1010001 8 10101100 3 1001000 9 00100001 12 001100 8 10111111 2 524286 0 1 00 10 01 11 000 100 010 110 001 101 011 111 0000 1000 0100 1100 0010 1010 0110 1110 0001 1001 0101 1101 0011 1011 0111 1111 00000 100...
output:
10 0 10 10 -7 0 3 13 13 10 9 -7 8 0 3 0 13 13 20 13 8 12 0 9 -7 -7 8 8 2 0 3 0 0 0 16 13 13 13 33 8 8 13 9 8 0 12 0 9 9 9 -7 -7 -8 -9 17 8 8 8 2 2 16 0 3 0 0 0 -3 0 0 0 3 25 13 13 8 13 13 13 33 24 6 8 8 8 14 15 17 8 8 8 0 0 0 12 0 9 9 9 9 9 8 7 -16 0 -7 -7 -8 -8 0 -9 17 8 8 8 8 8 8 10 -7 2 2 2 16 16...
result:
ok 524286 numbers
Test #4:
score: -100
Wrong Answer
time: 1066ms
memory: 209688kb
input:
18 01 12 100 19 110 17 011 8 0010 10 1101 20 1110 4 1011 19 00101 16 1000 10 001110 1 110010 7 0011111 11 01101011 8 0011111 9 10011110 11 011101 4 01011000 1 524286 0 1 00 10 01 11 000 100 010 110 001 101 011 111 0000 1000 0100 1100 0010 1010 0110 1110 0001 1001 0101 1101 0011 1011 0111 1111 00000 ...
output:
12 -6 11 11 0 6 11 8 9 11 1 26 8 10 11 2 10 8 9 9 9 11 1 16 26 27 8 1 12 10 11 2 0 2 10 10 10 8 9 -6 9 9 9 20 9 11 1 10 16 27 26 26 19 27 8 8 1 0 13 -1 12 10 11 2 0 2 0 0 0 2 10 10 10 10 10 4 10 8 9 0 -6 -10 9 9 9 9 9 9 20 9 9 24 9 11 1 10 10 10 6 16 20 27 26 22 26 27 19 0 19 27 8 16 8 8 1 1 0 0 13 ...
result:
wrong answer 230th numbers differ - expected: '12', found: '9'