QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#419697#8713. 不要玩弄字符串LynkcatWA 216ms312908kbC++203.9kb2024-05-24 09:53:332024-05-24 09:53:33

Judging History

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

  • [2024-05-24 09:53:33]
  • 评测
  • 测评结果:WA
  • 用时:216ms
  • 内存:312908kb
  • [2024-05-24 09:53:33]
  • 提交

answer

#include<bits/stdc++.h>
#define poly vector<int>
#define IOS ios::sync_with_stdio(false)
#define ll long long
#define mp make_pair
#define mt make_tuple
#define pa pair < int,int >
#define fi first
#define se second
#define inf 1e18
#define mod 998244353
#define sz(x) ((int)((x).size()))
#define int ll
// #define N 
using namespace std;
int dis[1<<18][150];
int tr[150][2];
int bl[150];
int val[25];
int coef[1<<18];
vector<pa>G[155];
int vis[155];
int fail[150];
int rt;
int n;
void BellaKira()
{
	int t;
	cin>>t;
	for (int i=0;i<t;i++)
	{
		string s;
		cin>>s;
		if (!rt) rt=++n;
		int now=rt;
		for (auto u:s)
		{
			if (!tr[now][u-'0']) tr[now][u-'0']=++n;
			now=tr[now][u-'0'];
		}
		bl[now]|=(1<<i);
		cin>>val[i];
	}

	//build 
	{
		for (int i=0;i<2;i++) tr[0][i]=1;
		queue<int>q;
		q.push(rt);
		while (!q.empty())
		{
			int u=q.front();
			q.pop();
			for (int i=0;i<2;i++)
				if (!tr[u][i]) tr[u][i]=tr[fail[u]][i];
				else 
				{
					// cout<<u<<"---->"<<tr[u][i]<<" "<<tr[fail[u]][i]<<" "<<fail[u]<<endl;
					fail[tr[u][i]]=tr[fail[u]][i],q.push(tr[u][i]);
				}
		}
		for (int i=1;i<=n;i++)
		{
			// cout<<i<<"->";
			for (int j=0;j<2;j++)
			{
				// cout<<tr[i][j]<<",";
				G[tr[i][j]].push_back(mp(i,j));
			}
			// cout<<fail[i];
			// cout<<endl;
		}
	}


	for (int i=0;i<(1<<t);i++)
		for (int j=0;j<t;j++)
			if ((i>>j)&1)
				coef[i]+=val[j];

	for (int s=(1<<t)-2;s>=0;s--)
	{
		priority_queue<pair<int,int>>q;
		queue<pair<int,int>>q1;
		for (int i=1;i<=n;i++) vis[i]=0;
		for (int i=1;i<=n;i++)
		{
			if ((bl[i]|s)!=s) continue;
			int oo=0;
			int op=-inf;
			for (int j=0;j<2;j++)
			{
				int u=tr[i][j];
				int ad=(bl[u]|s)-s;
				if (ad) 
				{
					op=max(-dis[bl[u]|s][u]+coef[ad],op);
					oo|=(1<<j);
				}
			}
			vis[i]=oo;
			if (oo==3) 
			{
				dis[s][i]=op;
				q1.push(mp(dis[s][i],i));
			} else
			if (op>0)
			{
				q.push(mp(op,i));
			}
		}
		// cout<<s<<" "<<vis[4]<<" "<<vis[6]<<" "<<(bl[6]|s)<<" "<<s<<endl;
		while (!q.empty()||!q1.empty())
		{
			if (!q1.empty())
			{
				int k=q1.front().se;
				q1.pop();
				// if (s==2)
					// cout<<s<<" "<<k<<","<<dis[s][k]<<" "<<vis[k]<<" "<<tr[k][0]<<" "<<tr[k][1]<<" "<<vis[6]<<endl;
				for (auto [x,y]:G[k])
				{
					if ((bl[x]|s)!=s) continue;
					if (vis[x]==3) continue;
					vis[x]|=(1<<y);
					if (vis[x]==3)
					{
						dis[s][x]=-inf;
						for (int j=0;j<2;j++)
						{
							int u=tr[x][j];
							int ad=(bl[u]|s)-s;
							dis[s][x]=max(-dis[bl[u]|s][u]+coef[ad],dis[s][x]);
						}
						q1.push(mp(dis[s][x],x));
					} else
					{
						if (dis[s][k]<0)
						{
							q.push(mp(-dis[s][k],x));
						}
					}
				}
			} else
			{
				auto [w,k]=q.top();
				q.pop();
				if (dis[s][k]>w) continue;
				dis[s][k]=w;
				// if (s==2)
					// cout<<"!"<<s<<" "<<k<<","<<dis[s][k]<<" "<<vis[k]<<" "<<tr[k][0]<<" "<<tr[k][1]<<" "<<vis[6]<<endl;
				vis[k]=3;
				for (auto [x,y]:G[k])
				{
					if ((bl[x]|s)!=s) continue;
					if (vis[x]==3) continue;
					vis[x]|=(1<<y);
					if (vis[x]==3)
					{
						dis[s][x]=-inf;
						for (int j=0;j<2;j++)
						{
							int u=tr[x][j];
							int ad=(bl[u]|s)-s;
							dis[s][x]=max(-dis[bl[u]|s][u]+coef[ad],dis[s][x]);
						}
						q1.push(mp(dis[s][x],x));
					} else
					{
						if (dis[s][k]<0)
						{
							q.push(mp(-dis[s][k],x));
						}
					}
				}
			}
		}
	}

	int q;
	cin>>q;
	while (q--)
	{
		string st;
		cin>>st;
		int now=rt;
		int s=0;
		int ad=0;
		for (auto u:st)
		{
			now=tr[now][u-'0'];
			ad+=coef[(s|bl[now])-s];
			s|=bl[now];
		}
		cout<<dis[s][now]<<'\n';
	}
}
signed main()
{
	IOS;
	cin.tie(0);
	int T=1;
	while (T--)
	{
		BellaKira();
	}
}
/*list:
1.mod 998244353 or 1e9+7 or ???
2.N
3.duipai shuju xingtai duoyidian
...
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 216ms
memory: 312908kb

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
77016
0
0
0
0
0
0
0
0
0
77190
0
0
0
31681
0
-77016
0
0
0
0
0
26277
0
84789
89827
84183
77016
77016
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
-84789
-77190
0
-77016
-77016
0
0
0
0
0
0
0
0
0
0
26277
26277
0
0
84789
...

result:

wrong answer 26th numbers differ - expected: '77190', found: '77016'