B. Balanced Substring

该文章介绍了一个问题,即在两个喜欢不同颜色的人之间找到一个字符串的最长子串,使得其中的0(代表黄色)和1(代表白色)数量相等。通过将0视为-1并进行前缀和处理,利用哈希映射存储前缀和出现的位置,从而找出满足条件的最长子串。给出的C++代码实现了解决方案。

传送门
题意:

kele妹妹喜欢黄色, 但是若愚姐姐喜欢白色, 她俩谁也说服不了对方, 于是她俩决定在颜色上面要做到均衡, 即两种颜色一定范围内出现的数量要相同才可以
现在用0代表kele妹妹喜欢的黄色,用1代表若愚姐姐喜欢的白色
给定一个由1和0组成的字符串, 希望找到一个最长的子串, 红色和白色的数量一致

思路:

因为是要找到一个最长的子串,并且其中的1和0的数量相同,那么可以把0当做-1处理,那么做前缀处理就是出现的前缀相同的时候两个范围内的区间是满足条件的,遍历一遍取max即可。

代码:


#include<cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include<vector>
#include<queue>
#include<map>
#define sc_int(x) scanf("%d", &x)
#define sc_ll(x) scanf("%lld", &x)
#define pr_ll(x) printf("%lld", x)
#define pr_ll_n(x) printf("%lld\n", x)
#define pr_int_n(x) printf("%d\n", x)
#define ll long long 
using namespace std;

const int N=1000000+100;
int n ,m,h;
char s[N];
int dp[N];

void slove( )
{
	sc_int(n);
	cin>>s+1;
	for(int i =1;i<=n;i++)
	{
		if(s[i]=='1') dp[i]=dp[i-1]+1;
		else dp[i]=dp[i-1]-1;
	}
	map<int,int>q;
	int res=0;
	q[0]=0;
	for(int i =1;i<=n;i++)
	{
		if(!q[dp[i]])q[dp[i]]=i;//只要纪录第一次出现的下标即可
		else res=max(res,i-q[dp[i]]);

		if(dp[i]==0)res=max(res,i);//因为如果为0的话,那么整个前缀和的长度都可以满足条件
	}
	cout<<res;
	cout<<endl;
}

int main()
{
	int t;
	// sc_int(t);
	// while(t--)
	slove();
	return 0;
}
C - Brackets Stack Query / Time Limit: 3 sec / Memory Limit: 1024 MiB Score : 300 points Problem Statement A string T is called a good bracket sequence if and only if it satisfies the following condition: T can be made into an empty string by repeating the following operation zero or more times: Choose () contained in T as a (contiguous) substring and remove it. For example, (), (()()), and the empty string are good bracket sequences, but )()( and ))) are not good bracket sequences. There is a string S. Initially, S is an empty string. Process Q queries in the order they are given. After each query, determine whether S is a good bracket sequence. There are two types of queries: 1 c: A character c is given. c is either ( or ). Append c to the end of S. 2: Remove the last character of S. It is guaranteed that S is not an empty string at this time. Constraints 1≤Q≤8×10 5 c in queries of the first type is ( or ). It is guaranteed that S is not empty when a query of the second type is given. Q is an integer. Input The input is given from Standard Input in the following format, where query i ​ denotes the i-th query. Q query 1 ​ query 2 ​ ⋮ query Q ​ Each query is given in one of the following two formats: 1 c 2 Output Output Q lines. The i-th line should contain Yes if the string S immediately after processing the i-th query is a good bracket sequence, and No otherwise. Sample Input 1 Copy 8 1 ( 2 1 ( 1 ) 2 1 ( 1 ) 1 ) Sample Output 1 Copy No Yes No Yes No No No Yes S immediately after processing the 1st query is (, which is not a good bracket sequence. S immediately after processing the 2nd query is an empty string, which is a good bracket sequence. S immediately after processing the 3rd query is (, which is not a good bracket sequence. S immediately after processing the 4th query is (), which is a good bracket sequence. S immediately after processing the 5th query is (, which is not a good bracket sequence. S immediately after processing the 6th query is ((, which is not a good bracket sequence. S immediately after processing the 7th query is ((), which is not a good bracket sequence. S immediately after processing the 8th query is (()), which is a good bracket sequence.c++
10-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值