CF EDU 110 C - Unstable String
作者:互联网
C - Unstable String
DP
状态表示:\(f[i][0/1]\) 为以第 \(i\) 个字符且这个字符为 \(0/1\) 结尾的子串的个数
状态转移见代码
因为统计的是有多少子串是稳定的,不是统计有多少稳定的字符串,所以一个位置上只能取 \(0/1\) 中最长的
所以答案为 \(\sum max(f[i][0],f[i][1])\)
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
const ll INF = 1e18;
string s;
ll f[N][2];
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int T;
cin >> T;
while(T--)
{
cin >> s;
int n = s.size();
s = " " + s;
ll ans = 0;
for (int i = 1; i <= n; i++)
{
if (s[i] == '0')
{
f[i][0] = f[i-1][1] + 1;
f[i][1] = 0;
}
else if (s[i] == '1')
{
f[i][0] = 0;
f[i][1] = f[i-1][0] + 1;
}
else
{
f[i][0] = f[i-1][1] + 1;
f[i][1] = f[i-1][0] + 1;
}
ans += max(f[i][0], f[i][1]);
}
cout << ans << endl;
}
return 0;
}
标签:const,String,int,ll,cin,Unstable,110,include 来源: https://www.cnblogs.com/hzy717zsy/p/16272612.html