QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#419490 | #8713. 不要玩弄字符串 | Lynkcat | WA | 0ms | 3668kb | C++20 | 3.3kb | 2024-05-23 23:19:47 | 2024-05-23 23:19:48 |
Judging History
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++)
{
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][i]+coef[ad],op);
oo|=(1<<j);
}
}
if (oo==3)
{
vis[i]=3;
dis[s][i]=op;
q1.push(mp(dis[s][i],i));
} else
if (op>0)
{
q.push(mp(op,i));
}
}
while (!q.empty()||!q1.empty())
{
if (!q1.empty())
{
int k=q1.front().se;
q1.pop();
for (auto [x,y]:G[k])
{
if (vis[x]==3) continue;
vis[x]|=(1<<y);
if (vis[x]==3)
{
dis[s][x]=max(-dis[s][tr[x][0]],-dis[s][tr[x][1]]);
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;
vis[k]=3;
for (auto [x,y]:G[k])
{
if (vis[x]==3) continue;
vis[x]|=(1<<y);
if (vis[x]==3)
{
dis[s][x]=min(-dis[s][tr[x][0]],-dis[s][tr[x][1]]);
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];
}
// cerr<<"query "<<s<<" "<<now<<" "<<dis[s][now]<<endl;
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
...
*/
详细
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3668kb
input:
3 11 1 0 2 000 3 2 0 1
output:
-3 3
result:
wrong answer 1st numbers differ - expected: '-1', found: '-3'