Educational Codeforces Round 110 (Rated for Div. 2) C. Unstable String(状态机)

https://codeforces.com/contest/1535/problem/C

题目大意:

给定一个字符串s,由 1 0 ?组成:?每次都可以任意替换成0或者1

问我们这个子字符串中,能够组成010101这样两两互不相等的字符串的数量最大是多少?
input 
3
0?10
???
?10??1100
output 
8
6
25

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL N=2e5+7;
LL f[N][2];
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    cin>>T;
    while(T--)
    {
        memset(f,0,sizeof f);
        string s;
        cin>>s;
        s="?"+s;
        LL ans=0;
        for(LL i=1;i<s.size();i++)
        {
            if(s[i]=='0') f[i][1]=f[i-1][0]+1;
            else if(s[i]=='1') f[i][0]=f[i-1][1]+1;
            else
            {
                f[i][1]=f[i-1][0]+1;
                f[i][0]=f[i-1][1]+1;
            }
            ans+=max(f[i][1],f[i][0]);
        }
        cout<<ans<<endl;
    }
    return 0;
}
posted @ 2023-04-14 20:16  高尔赛凡尔娟  阅读(12)  评论(0编辑  收藏  举报