CodeForces - 1535C Unstable String(思维)

Posted Frozen_Guardian

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces - 1535C Unstable String(思维)相关的知识,希望对你有一定的参考价值。

题目链接:点击查看

题目大意:规定一个字符串将问号都替换成 0 0 0 1 1 1 后满足 01 01 01 交替的话,该字符串是合法的,现在给出一个长度为 n n n 的字符串,求合法子串的个数

题目分析:两种思路,第一种也是比赛里想到的,不太会实现细节,就是尺取每两个合法 01 01 01 段,然后对两端连续的问号段容斥去重,个人感觉比较难写

考虑子串就是每个前缀的所有后缀,所以枚举点 i i i 作为每个前缀 s [ 1 : i ] s[1:i] s[1:i],找到其合法的最长的后缀所代表的左端点 l l l,使得 [ l , i ] [l,i] [l,i] 这段区间是合法的,那么显然 [ l , i ] [l,i] [l,i] 这段区间内的任意点作为子串的左端点都是合法的,累加即可

不难发现上一段中提到的 l l l 是递增的,所以考虑如何简单的维护

我们可以将每个位置的下标和 01 01 01 串结合,不难发现,一段合法的子串,其 i + s i i+s_i i+si 的值奇偶性一定是相同的

所以我们按照其奇偶性维护一下两种情况最后一次出现的位置就好了

代码:

// Problem: C. Unstable String
// Contest: Codeforces - Educational Codeforces Round 110 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1535/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#define lowbit(x) x&-x
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
template<typename T>
inline void read(T &x)
{
    T f=1;x=0;
    char ch=getchar();
    while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
    x*=f;
}
template<typename T>
inline void write(T x)
{
    if(x<0){x=~(x-1);putchar('-');}
    if(x>9)write(x/10);
    putchar(x%10+'0');
}
const int inf=0x3f3f3f3f;
const int N=1e6+100;
char s[N];
int main()
{
#ifndef ONLINE_JUDGE
//	freopen("data.in.txt","r",stdin);
//	freopen("data.out.txt","w",stdout);
#endif
//	ios::sync_with_stdio(false);
	int w;
	cin>>w;
	while(w--) {
		scanf("%s",s+1);
		int n=strlen(s+1);
		LL ans=0;
		int last[2]={0,0};
		for(int i=1;i<=n;i++) {
			if(s[i]!='?') {
				last[(i+s[i]-'0')%2]=i;
			}
			ans+=i-min(last[0],last[1]);
		}
		cout<<ans<<endl;
	}
    return 0;
}

以上是关于CodeForces - 1535C Unstable String(思维)的主要内容,如果未能解决你的问题,请参考以下文章

如何看codeforces做了多少题

codeforces上怎么看测试数据

codeforces比赛后怎么看题解和答案

codeforces是啥?

codeforces Codeforces 650A Watchmen

CodeForces - 504A && CodeForces - 624C && CodeForces - 2B