NC22427. Immortal … Universe
描述
输入描述
The first line contains a string s of length, consisting of only ``P'', ``V'' and ``?''.
The second line contains a string t of length, consisting of only ``P'', ``V'' and ``?''.
s and t make up Rikka's information.
输出描述
Output a single integer, the number of possible trends which will never lead the boy to bankruptcy, modulo 998244353.
示例1
输入:
PV ??
输出:
1
示例2
输入:
??? P?P
输出:
3
示例3
输入:
????? ?????
输出:
326
C++11(clang++ 3.9) 解法, 执行用时: 1200ms, 内存消耗: 876K, 提交时间: 2020-03-13 12:50:36
#include<iostream> #include<algorithm> #include<cstring> using namespace std; typedef long long ll; const int mod=998244353; #define MAXNUM 5555 int dps[2][2][MAXNUM*2],dpt[2][2][MAXNUM*2]; char ss[MAXNUM],st[MAXNUM]; int n,m,type1,type2; void add(int &a,int b) { a+=b; if(a>=mod) a-=mod; } #define rep(i,s,t) for(int i=s;i<t;i++) void getdp(char s[],int n,int dp[][2][MAXNUM*2],int &type) { reverse(s+1,s+1+n); type=1; dp[0][0][n]=1; rep(i,1,n+1) { memset(dp[type],0,sizeof(dps[0])); rep(j,0,2*n+1) { if(s[i]!='P') { rep(k,0,2) add(dp[type][k][j+1],dp[!type][k][j]); } if(s[i]!='V'&&j) { if(j-1>=n) add(dp[type][1][n],dp[!type][0][j]); else add(dp[type][0][j-1],dp[!type][0][j]); add(dp[type][1][min(n,j-1)],dp[!type][1][j]); } } type=!type; } } int main() { scanf("%s%s",ss+1,st+1); n=strlen(ss+1),m=strlen(st+1); getdp(ss,n,dps,type1),getdp(st,m,dpt,type2); int res=0; rep(i,0,2*n+1) rep(j,0,2*m+1) { if(i+j>n+m) add(res,(dps[!type1][0][i]+dps[!type1][1][i])*1LL*(dpt[!type2][0][j]+dpt[!type2][1][j])%mod); else if(i+j==n+m) add(res,dps[!type1][0][i]*1LL*dpt[!type2][0][j]%mod); } printf("%d\n",res); }