QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#417744 | #8713. 不要玩弄字符串 | light_ink_dots# | WA | 293ms | 157292kb | C++14 | 3.2kb | 2024-05-22 21:33:20 | 2024-05-22 21:33:21 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
int main() {
static const int maxk = 18, maxu = 150;
int k;
scanf("%d", &k);
static int v[maxk];
static int nxt[maxu][2], id[maxu], cnt = 1;
memset(id, -1, sizeof(id)), nxt[1][0] = ++cnt, nxt[1][1] = ++cnt;
for (int i = 0; i < k; i++) {
static char s[maxu];
scanf("%s", s + 1), scanf("%d", v + i);
int l = strlen(s + 1), u = 1;
for (int p = 1; p <= l; p++) {
int d = s[p] == '1';
if (!nxt[u][d])
nxt[u][d] = ++cnt;
u = nxt[u][d];
}
if (id[u] != -1) {
v[id[u]] += v[i], i--, k--;
continue;
}
id[u] = i;
}
queue<int> Q;
static int fa[maxu];
for (int i : nxt[1])
if (i)
fa[i] = 1, Q.push(i);
while (!Q.empty()) {
int u = Q.front();
Q.pop();
for (int d = 0; d < 2; d++)
if (nxt[u][d])
fa[nxt[u][d]] = nxt[fa[u]][d], Q.push(nxt[u][d]);
else
nxt[u][d] = nxt[fa[u]][d];
}
static int dp[1 << maxk][maxu];
for (int s = (1 << k) - 2; s >= 0; s--) {
static int deg[maxu];
static vector<int> G[maxu];
memset(dp[s], 0xcf, sizeof(dp[s]));
for (int u = 1; u <= cnt; u++) G[u].clear();
for (int u = 1; u <= cnt; u++) {
deg[u] = 0;
int t0 = nxt[u][0], t1 = nxt[u][1];
if (id[t0] != -1 && !((s >> id[t0]) & 1))
dp[s][u] = max(dp[s][u], -dp[s ^ (1 << id[t0])][t0] + v[id[t0]]);
else
G[t0].push_back(u), deg[u]++;
if (id[t1] != -1 && !((s >> id[t1]) & 1))
dp[s][u] = max(dp[s][u], -dp[s ^ (1 << id[t1])][t1] + v[id[t1]]);
else
G[t1].push_back(u), deg[u]++;
}
struct node {
int u, d;
inline bool operator<(const node rhs) const { return abs(d) < abs(rhs.d); }
};
priority_queue<node> Q;
for (int u = 1; u <= cnt; u++)
if (dp[s][u] >= 0)
Q.push({ u, dp[s][u] });
static bool vis[maxu];
memset(vis, 0, sizeof(vis));
while (!Q.empty()) {
int u = Q.top().u;
Q.pop();
if (vis[u])
continue;
vis[u] = true;
for (int i : G[u]) {
int pre = dp[s][i];
dp[s][i] = max(dp[s][i], -dp[s][u]), deg[i]--;
if (!deg[i] || (dp[s][i] >= 0 && dp[s][i] != pre))
Q.push({ i, dp[s][i] });
}
}
for (int u = 1; u <= cnt; u++)
if (deg[u])
dp[s][u] = max(dp[s][u], 0);
}
int q;
scanf("%d", &q);
while (q--) {
static char s[maxu];
scanf("%s", s + 1);
int l = strlen(s + 1), S = 0, u = 1, val = 0;
for (int i = 1; i <= l; i++) {
int d = s[i] == '1';
u = nxt[u][d];
if (id[u] != -1 && !((S >> id[u]) & 1))
S ^= 1 << id[u];
}
printf("%d\n", dp[S][u]);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3688kb
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: 293ms
memory: 157292kb
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 45298 0 84183 0 0 0 0 0 0 0 0 0 0 77190 0 0 0 31681 0 0 0 0 0 0 45298 0 0 84789 89827 84183 0 0 -45298 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 -84789 -77190 0 0 0 45298 0 0 53902 0 0 0 0 45298 45298 0 0 0 0 84789 84789 8982...
result:
wrong answer 23rd numbers differ - expected: '0', found: '45298'