Problem:
You are given a string s
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 s
.
Input:
The first line contains a single integer t
(1≤t≤104
) — number of test cases.
The first and only line of each test case contains the string s
(1≤|s|≤2⋅105
) consisting of characters 0, 1, and ?.
It is guaranteed that the sum of the string lengths over all test cases does not exceed 2⋅105
.
Output:
For each test case, output a single integer — the number of beautiful substrings of the string s
.
Example:
Input:
3
0?10
???
?10??1100
Output:
8
6
25
思路:
模拟+思维,从第二位开始,判断符合题目要求的连续字符串长度,’?‘先变为与前一位不同的数,到不满足时,还原为原字符串,第一位为’?'的情况特殊处理,详见code。
code:
#include <bits/stdc++.h>
#define DEBUG freopen("_in.txt", "r", stdin);
// #define DEBUG freopen("_in.txt", "r", stdin),freopen("_out.txt", "w", stdout);
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const ll maxn = 2e5 + 10;
const ll mod = 2147493647;
ll t, cnt, sum;
string str, str1;
int main()
{
// DEBUG;
cin >> t;
while (t--)
{
cin >> str;
str1 = str;
ll len = str.length();
cnt = 1;
sum = 0;
if (str[0] == '?')
{
str[0] = '0';
ll sum1 = 0, sum2 = 0;
for (ll i = 1; i < len; i++)
{
if (str[i] == '?')
{
cnt++;
if (str[i - 1] == '1')
str[i] = '0';
else
str[i] = '1';
}
else if (str[i] != str[i - 1])
{
cnt++;
}
else
{
// cout<<cnt<<" ";
sum1 += cnt * (cnt + 1) / 2;
cnt = 1;
str = str1;
if (str[i - 1] == '?')
{
ll pos = i - 1,num=0;
while (pos > 0 && str[pos] == '?')
{
cnt++;
num++;
pos--;
}
sum1-=num*(num+1)/2;
}
}
}
sum1 += cnt * (cnt + 1) / 2;
str[0]='1';
cnt=1;
for (ll i = 1; i < len; i++)
{
if (str[i] == '?')
{
cnt++;
if (str[i - 1] == '1')
str[i] = '0';
else
str[i] = '1';
}
else if (str[i] != str[i - 1])
{
cnt++;
}
else
{
sum2 += cnt * (cnt + 1) / 2;
cnt = 1;
str = str1;
if (str[i - 1] == '?')
{
ll pos = i - 1,num=0;
while (pos > 0 && str[pos] == '?')
{
cnt++;
num++;
pos--;
}
sum2-=num*(num+1)/2;
}
}
}
sum2 += cnt * (cnt + 1) / 2;
cout<<max(sum1,sum2)<<endl;
continue;
}
for (ll i = 1; i < len; i++)
{
if (str[i] == '?')
{
cnt++;
if (str[i - 1] == '1')
str[i] = '0';
else
str[i] = '1';
}
else if (str[i] != str[i - 1])
{
cnt++;
}
else
{
sum += cnt * (cnt + 1) / 2;
cnt = 1;
str = str1;
if (str[i - 1] == '?')
{
ll pos = i - 1,num=0;
while (pos > 0 && str[pos] == '?')
{
cnt++;
num++;
pos--;
}
sum-=num*(num+1)/2;
}
}
}
sum += cnt * (cnt + 1) / 2;
cout << sum << endl;
}
return 0;
}