QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#417188 | #8713. 不要玩弄字符串 | zhoukangyang# | WA | 258ms | 186644kb | C++14 | 3.1kb | 2024-05-22 15:54:17 | 2024-05-22 15:54:18 |
Judging History
answer
#include<bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); ++i)
#define R(i, j, k) for(int i = (j); i >= (k); --i)
#define ll long long
#define sz(a) ((int) (a).size())
#define vi vector < int >
#define me(a, x) memset(a, x, sizeof(a))
#define ull unsigned long long
#define ld __float128
#define pb emplace_back
using namespace std;
const int N = 1 << 18;
const int M = 150;
int k, q, v[N];
string s[N];
int dp[N][M];
int ch[N][2], fa[N], tot;
int seq[N], dep[N];
int ord[N];
void build() {
queue<int>q;
L(i, 0, 1) if(ch[0][i]) q.push(ch[0][i]);
while(!q.empty()) {
int u = q.front();
q.pop();
L(j, 0, 1)
if(ch[u][j]) fa[ch[u][j]] = ch[fa[u]][j], q.push(ch[u][j]);
else ch[u][j] = ch[fa[u]][j];
}
}
vi e[N];
int f[N];
int vis[N], deg[N];
int ss[N];
vi LST[N];
int main() {
ios :: sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> k;
L(i, 0, k - 1) {
cin >> s[i] >> v[i];
int cur = 0;
L(j, 0, sz(s[i]) - 1) {
if(!ch[cur][s[i][j] - '0']) ch[cur][s[i][j] - '0'] = ++tot;
cur = ch[cur][s[i][j] - '0'];
dep[cur] = j + 1;
}
seq[cur] |= 1 << i;
}
build();
L(i, 1, tot) {
ord[i] = i;
}
sort(ord + 1, ord + tot + 1, [&] (int x, int y) {
return dep[x] < dep[y];
});
L(i, 1, tot)
seq[ord[i]] |= seq[fa[ord[i]]];
// L(i, 0, tot) {
// L(j, 0, 1) {
// cout << ch[i][j] << ' ';
// }
// cout << endl;
// }
// return 0;
L(i, 0, (1 << k) - 1)
L(j, 0, k - 1)
if(i >> j & 1)
f[i] += v[j];
L(i, 0, tot)
L(j, 0, 1)
LST[ch[i][j]].pb(i);
L(i, 0, (1 << k) - 1) {
L(j, 0, tot) {
dp[i][j] = -1e9;
if(seq[j] & i) {
int r = seq[j] & i;
ss[j] = f[r] - dp[i - r][j];
// cout << "j = " << j << endl;
// if(ss[j] > 0) cout << j << " qwq" << endl;
vis[j] = 0;
} else {
vis[j] = 1;
}
}
priority_queue<pair<int,int>>pq;
L(j, 0, tot) if(vis[j]) {
deg[j] = 2;
L(o, 0, 1) {
int to = ch[j][o];
if(!vis[to]) {
dp[i][j] = max(dp[i][j], ss[to]);
// if(ss[to] > 0) {
// cout << i << " and " << j << endl;
// }
--deg[j];
}
}
if(dp[i][j] > 0 || !deg[j]) pq.push({dp[i][j], j});
}
vi can(tot + 1);
while(!pq.empty()) {
int u = pq.top().second;
pq.pop();
if(dp[i][u] <= 0 && deg[u]) {
continue;
}
if(can[u])continue;
// if(i == 5) {
// cout << "u = " << u << ' ' << << endl;
// }
can[u] = 1;
for(auto&v : LST[u]) if(vis[v]) {
--deg[v], dp[i][v] = max(dp[i][v], -dp[i][u]);
if(dp[i][v] > 0 || !deg[v]) pq.push({dp[i][v], v});
}
}
L(j, 0, tot) if(dp[i][j] < -1e8 && deg[j]) dp[i][j] = 0;
// L(j, 0, tot) {
// cout << i << ' ' << j << " : " << dp[i][j] << endl;
// }
}
// cout << "DP = " << dp[5][3] << ' ' << dp[5][ch[3][1]] << endl;
cin >> q;
while(q--) {
string S;
cin >> S;
int cur = 0, mask = (1 << k) - 1;
L(i, 0, sz(S) - 1) {
cur = ch[cur][S[i] - '0'];
mask -= mask & seq[cur];
}
cout << dp[mask][cur] << '\n';
}
return 0;
}
/*
1
3 2
2023 40 41
1 1 2022
2 2 39
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 32260kb
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: 258ms
memory: 186644kb
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 -108871 0 0 0 0 -192880 -96830 0 108871 0 0 0 0 0 -53108 -226492 -108871 -96830 -96830 0 -31681 192880 96830 -93542 0 0 0 0 0 0 0 0 108871 -53108 -53108 -226492 31681 -108697 -96830 -96830 -96830 -96830 -128131 0 -25255 -31681 53108 192880 192880 96830 96830 -93542 -93542 0 0 0 0 0 0 0 0 0 0 0...
result:
wrong answer 4th numbers differ - expected: '0', found: '-108871'