QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#432064#8713. 不要玩弄字符串A_zjzjWA 473ms158712kbC++142.4kb2024-06-06 17:35:422024-06-06 17:35:43

Judging History

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

  • [2024-06-06 17:35:43]
  • 评测
  • 测评结果:WA
  • 用时:473ms
  • 内存:158712kb
  • [2024-06-06 17:35:42]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include"debug.h"
#else
#define debug(...) void()
#endif
#define all(x) (x).begin(),(x).end()
template<class T>
auto ary(T *a,int l,int r){
	return vector<T>{a+l,a+1+r};
}
using ll=long long;
using ull=unsigned ll;
const int N=18,M=1<<N|10,K=150,INF=1e9;
int n,q,a[M];
int k,ch[K][2],fail[K],s[K];
char str[N];
void insert(int id){
	int u=0,len=strlen(str);
	for(int i=0;i<len;i++){
		int &v=ch[u][str[i]&1];
		if(!v)v=++k;
		u=v;
	}
	s[u]|=1<<id;
}
void getfail(){
	queue<int>q;
	for(int i:{0,1}){
		int v=ch[0][i];
		if(v)q.push(v);
	}
	// for(int u=0;u<=k;u++){
	// 	debug(ary(ch[u],0,1));
	// }
	for(int u;!q.empty();){
		u=q.front(),q.pop();
		s[u]|=s[fail[u]];
		for(int i:{0,1}){
			int &v=ch[u][i],x=ch[fail[u]][i];
			if(v)fail[v]=x,q.push(v);
			else v=x;
		}
	}
	// for(int u=0;u<=k;u++){
	// 	debug(s[u],ary(ch[u],0,1));
	// }
}
int U,f[M][K],in[K],vis[K];
vector<int>to[K];
void init(){
	U=(1<<n)-1;
	for(int S=1;S<=U;S++){
		a[S]=a[S-(S&-S)]+a[S&-S];
	}
	for(int S=U;S>=0;S--){
		for(int u=0;u<=k;u++)to[u].clear();
		priority_queue<pair<int,int>>q;
		fill(f[S],f[S]+1+k,-INF);
		fill(vis,vis+1+k,0);
		fill(in,in+1+k,0);
		for(int u=0;u<=k;u++){
			if((S|s[u])!=S)continue;
			for(int i:{0,1}){
				int v=ch[u][i];
				// debug(S,u,v);
				if((S|s[v])!=S){
					f[S][u]=max(f[S][u],a[s[v]&~S]-f[S|s[v]][v]);
					q.push({a[s[v]&~S]-f[S|s[v]][v],u});
				}else to[v].push_back(u),in[u]++;
			}
		}
		function<void(int)>dfs=[&](int u){
			if(vis[u])return;
			vis[u]=1;
			for(int v:to[u]){
				f[S][v]=max(f[S][v],-f[S][u]);
				if(!--in[v])dfs(v);
				else q.push({-f[S][u],u});
			}
		};
		for(int u=0;u<=k;u++){
			if((S|s[u])!=S)continue;
			if(!in[u])dfs(u);
		}
		for(;!q.empty();){
			auto [w,u]=q.top();
			q.pop();
			if(w<0)break;
			dfs(u);
		}
		// debug(S,ary(f[S],0,k));
		for(int u=0;u<=k;u++){
			if(!vis[u])f[S][u]=max(f[S][u],0);
		}
		// debug(S,ary(f[S],0,k));
	}
}
int main(){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%s%d",str,&a[1<<i]),insert(i);
	}
	getfail(),init();
	scanf("%d",&q);
	for(;q--;){
		scanf("%s",str);
		int u=0,len=strlen(str),S=0;
		for(int i=0;i<len;i++){
			u=ch[u][str[i]&1],S|=s[u];
		}
		// debug(S,u);
		printf("%d\n",f[S][u]);
	}
	return 0;
}
#ifdef DEBUG
#include"debug.hpp"
#endif

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3804kb

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: 473ms
memory: 158712kb

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:

wrong answer 393rd numbers differ - expected: '58928', found: '71575'