这是一道思路题,话说这次集训特别喜欢考思路题
我们考虑一个字符串什么时候不合法
当且仅当当前值为0且两边均为P时
然后我们发现当我们遇到P时我们是可以一直保留P的
所以问题转化为对于两个字符串,分别求出f[i]表示前缀和最小为i且前缀和最小时,后一个字母为P
此时的方案数是多少
然后两个字符串的f[i]和f[j],如果i+j>=0,对答案就有f[i]*f[j]的贡献
那么我们考虑怎么求出f数组
我们可以倒着dp
如果到达一个P的时候,它的贡献为负数,那么他就可以继续下去发挥作用
否则如果为正数,当我们再次遇到一个P的时候,这个P肯定更加优秀,更容易卡掉前缀和,所以重新赋值为0
如果是V,就直接转移即可
然后最难的DP就已经被处理完了
还要判一下最后是空的还是P,用0/1表示以结尾还是以P结束
贴上代码
#include<bits/stdc++.h>
using namespace std;
const int mod=998244353;
int read()
{
char c;
int x;
for(c=getchar();c!='-'&&(c>'9'||c<'0');c=getchar());
if(c=='-')
{
x=0;
for(c=getchar();c>='0'&&c<='9';c=getchar())
x=x*10+c-'0';
return -x;
}
else
{
x=c-'0';
for(c=getchar();c>='0'&&c<='9';c=getchar())
x=x*10+c-'0';
return x;
}
}
void moc(int &x,int y)
{
x+=y;
if(x>=mod) x-=mod;
}
char s[2][5005];
int num[2][5005],f[2][2][10005][2],g[2][10005][2],len[2];
int main(){
int i,j,k;
for(int t=0;t<=1;t++)
{
scanf("%s",s[t]+1);
len[t]=strlen(s[t]+1);
f[t][(len[t]+1)&1][5000][0]=1;
for(i=len[t];i>=0;i--)
{
int now=i&1;
int pre=now^1;
memset(f[t][now],0,sizeof(f[t][now]));
for(j=5000+i-len[t];j<=5000+len[t]-i;j++)
{
if(s[t][i]=='?'||s[t][i]=='P')
{
if(j-1<5000)
{
moc(f[t][now][j-1][0],f[t][pre][j][0]);
moc(f[t][now][j-1][1],f[t][pre][j][1]);
}
else
{
moc(f[t][now][5000][1],f[t][pre][j][0]);
moc(f[t][now][5000][1],f[t][pre][j][1]);
}
}
if(s[t][i]=='?'||s[t][i]=='V')
{
moc(f[t][now][j+1][0],f[t][pre][j][0]);
moc(f[t][now][j+1][1],f[t][pre][j][1]);
}
}
}
for(j=5000-len[t];j<=5000+len[t];j++)
{
moc(g[t][j][0],f[t][1][j][0]);
moc(g[t][j][1],f[t][1][j][1]);
}
}
int ans=0;
for(int i=5000-len[0];i<=5000+len[0];i++)
{
for(int j=0;j<=1;j++)
{
for(int k=5000-len[1];k<=5000+len[1];k++)
{
for(int l=0;l<=1;l++)
{
if(i+k-10000-(j||l)>=0) moc(ans,1ll*g[0][i][j]*g[1][k][l]%mod);
}
}
}
}
printf("%d\n",ans);
return 0;
}