codeforces 991D - Bishwock (dp)
题目链接:https://codeforces.com/problemset/problem/991/D
\(dp[i][0/1][0/1]\) 表示第 i 列障碍物的状态
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<stack>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn = 110;
const int INF = 1000000007;
int n;
int mp[3][maxn],dp[maxn][2][2]; // 第 i 列,状态为 0无/1有的方案数
char s[maxn];
ll read(){ ll s=0,f=1; char ch=getchar(); while(ch<'0' || ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0' && ch<='9'){ s=s*10+ch-'0'; ch=getchar(); } return s*f; }
int main(){
scanf("%s",s+1);
n = strlen(s+1);
for(int i=1;i<=n;++i){
if(s[i] == '0') mp[1][i] = 0;
else mp[1][i] = 1;
}
scanf("%s",s+1);
for(int i=1;i<=n;++i){
if(s[i] == '0') mp[2][i] = 0;
else mp[2][i] = 1;
}
if(!mp[1][1] && mp[2][1]) dp[1][1][0] = dp[1][0][0] = -INF;
else if(mp[1][1] && !mp[2][1]) dp[1][0][1] = dp[1][0][0] = -INF;
else if(mp[1][1] && mp[2][1]) dp[1][0][0] = dp[1][0][1] = dp[1][1][0] = -INF;
for(int i=2;i<=n;++i){
if(!mp[1][i] && !mp[2][i]){
dp[i][0][0] = max(max(dp[i-1][0][0],dp[i-1][0][1]),max(dp[i-1][1][0],dp[i-1][1][1]));
dp[i][0][1] = max(max(dp[i-1][0][0] + 1,dp[i-1][0][1]),max(dp[i-1][1][0],dp[i-1][1][1]));
dp[i][1][0] = max(max(dp[i-1][0][0] + 1,dp[i-1][0][1]),max(dp[i-1][1][0],dp[i-1][1][1]));
dp[i][1][1] = max(max(dp[i-1][0][0] + 1, dp[i-1][0][1] +1), max(dp[i-1][1][0] + 1,dp[i-1][1][1]));
}else if(!mp[1][i] && mp[2][i]){
dp[i][0][1] = max(max(dp[i-1][0][0],dp[i-1][0][1]),max(dp[i-1][1][0],dp[i-1][1][1]));
dp[i][1][1] = max(max(dp[i-1][0][0] + 1,dp[i-1][0][1]),max(dp[i-1][1][0],dp[i-1][1][1]));
dp[i][1][0] = dp[i][0][0] = -INF;
}else if(mp[1][i] && !mp[2][i]){
dp[i][1][0] = max(max(dp[i-1][0][0],dp[i-1][0][1]),max(dp[i-1][1][0],dp[i-1][1][1]));
dp[i][1][1] = max(max(dp[i-1][0][0] + 1,dp[i-1][0][1]),max(dp[i-1][1][0],dp[i-1][1][1]));
dp[i][0][1] = dp[i][0][0] = -INF;
}else{
dp[i][1][1] = max(max(dp[i-1][0][0],dp[i-1][0][1]),max(dp[i-1][1][0],dp[i-1][1][1]));
dp[i][0][0] = dp[i][0][1] = dp[i][1][0] = -INF;
}
}
printf("%d\n",max(max(dp[n][0][0],dp[n][0][1]),max(dp[n][1][0],dp[n][1][1])));
return 0;
}