C. Unstable String(dp01串)

传送门

You are given a string ss consisting of the characters 0, 1, and ?.

Let's call a string unstable if it consists of the characters 0 and 1 and any two adjacent characters are different (i. e. it has the form 010101... or 101010...).

Let's call a string beautiful if it consists of the characters 0, 1, and ?, and you can replace the characters ? to 0 or 1 (for each character, the choice is independent), so that the string becomes unstable.

For example, the strings 0??10, 0, and ??? are beautiful, and the strings 00 and ?1??1 are not.

Calculate the number of beautiful contiguous substrings of the string ss.

Input

The first line contains a single integer tt (1≤t≤1e4) — number of test cases.

The first and only line of each test case contains the string ss (1|s|2⋅1e5) consisting of characters 0, 1, and ?.

It is guaranteed that the sum of the string lengths over all test cases does not exceed 21e5

Output

For each test case, output a single integer — the number of beautiful substrings of the string ss.

Example
input
Copy
3
0?10
???
?10??1100
output
Copy
8
6
25


题意:

给一个字符串,其含有’0’,‘1’,’?’. 其中,’?‘可以看成’1’,也可以看成’0’。如果一个字符串可以是0和1交替出现的,就称这个字符串是beautifulbeautiful。问现在这个字符串有多少beautiful的子串?

例如,
字符串1,10是beautiful的;
字符串1???1也是beautiful的,因为通过问号可以转化为10101;
但字符串1???1不是beautiful的,因为通过问号不管怎么变,都不可能使得01交替出现。

由于这里之后01所以数组的第二维只能是两种0/1

dp[i][0]代表的是第i为是0的并且选择第i为的最长的好串
dp[i][1]代表的是第i为是1的并且选择第i为的最长的好串

注意了我们这个dp[i][0]是指的是包含i这个字符,

你会发现如果dp[i][0]==4的话,代表的是以0结尾的并且有四位01交替的串,并且它的字串的数量也是4,举个例子:

dp[i][0]=4代表的1010这个串,你会发现这个0,01,010和1010都是它的满足题意的字串。所以说每一个的贡献就是max(dp[i][[0],dp[i][1])

如果a[i]==‘?’的话,你只能选择一个也是max(dp[i][[0],dp[i][1])

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
const int maxn=3e5+100;
typedef long long ll;//数位 
int dp[maxn][2];//dp[i][0]代表的是第i为是0的并且选择第i为的最长的好串 
char a[maxn];//dp[i][1]代表的是第i为是1的并且选择第i为的最长的好串  
int main(){
    int t;
    cin>>t;
    while(t--){
        scanf("%s",a+1);
        memset(dp,0,sizeof(dp));
        int n=strlen(a+1); 
        ll ans=0;
        for(int i=1;i<=n;i++){
            if(a[i]=='0'){
                dp[i][0]=dp[i-1][1]+1;
                ans+=dp[i][0];
            }
            else if(a[i]=='1'){
                dp[i][1]=dp[i-1][0]+1;
                ans+=dp[i][1];
            }
            else{
                dp[i][0]=dp[i-1][1]+1;
                dp[i][1]=dp[i-1][0]+1;
                ans+=max(dp[i][0],dp[i][1]);
            }
        }
        cout<<ans<<"\n"; 
    }
}

 

posted @ 2021-06-06 23:34  lipu123  阅读(115)  评论(0)    收藏  举报