CSU 1809 括号匹配问题

本文介绍了一种判断括号序列在交换两个位置后的合法性问题,通过两种不同的实现方式——暴力求解与RMQ区间最小值维护,来快速响应查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Description

Bobo has a balanced parenthesis sequence P=p 1 p 2…p n of length n and q questions.
The i-th question is whether P remains balanced after p ai and p bi  swapped. Note that questions are individual so that they have no affect on others.
Parenthesis sequence S is balanced if and only if:
1. S is empty;
2. or there exists balanced parenthesis sequence A,B such that S=AB;
3. or there exists balanced parenthesis sequence S' such that S=(S').

Input

The input contains at most 30 sets. For each set:
The first line contains two integers n,q (2≤n≤10 5,1≤q≤10 5).
The second line contains n characters p 1 p 2…p n.
The i-th of the last q lines contains 2 integers a i,b i (1≤a i,b i≤n,a i≠b i).

Output

For each question, output " Yes" if P remains balanced, or " No" otherwise.

Sample Input

4 2
(())
1 3
2 3
2 1
()
1 2

Sample Output

No
Yes
No


有个wrong点就是 l,r  ,l不一定比r小,这个要自己先固定下来。。。。。

因为原串是合法序列,那么交换两个位置,我们有以下几类讨论:

1. 符号相等的,直接yes

2.原先是)(类型的,交换后()所以也直接yes

3.( ) 交换后就要考虑情况了!...


()()()  , 交换后,从l开始后面匹配的括号,(的数量就要减少2个,这个要自己深入去理解,所以我们要维护区间最小值,即【l,r) 的最小值

如果该数>=2 ,输出yes,否则就输出NO


这个我是直接暴力过去的,直接让我水过去了。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1e5+100;
char s[N];
int pre[N];
int main()
{
	int n,m,i,j,len;
	int l,r;
	while(scanf("%d%d",&n,&m)!=EOF) {
		memset(pre,0,sizeof(pre));
		scanf("%s",s+1);
		for(i=1;i<=n;i++) {
			pre[i]+=pre[i-1];
			if(s[i]=='(') pre[i]++;
			else pre[i]--;
		}
		while(m--) {
			cin>>l>>r;
			if(l>r) swap(l,r);
			if(s[l]==s[r]) printf("Yes\n");
			else if(s[l]==')') printf("Yes\n");
			else {
				for(i=l;i<r;i++) {
					if(pre[i]-2<0) break;
				}
				if(i==r) printf("Yes\n");
				else printf("No\n");
			}
		}
	}
	return 0;
}

这个是用RMQ维护的:


#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-6;
void File()
{
    freopen("D:\\in.txt","r",stdin);
    freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
    char c = getchar(); x = 0;while(!isdigit(c)) c = getchar();
    while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar();  }
}
 
const int maxn=100010;
int n,m;
char s[maxn];
int sum[maxn];
int dp[maxn][30];
 
void RMQ_init()
{
    for(int i=0;i<n;i++) dp[i][0]=sum[i];
    for(int j=1;(1<<j)<=n;j++)
        for(int i=0;i+(1<<j)-1<n;i++)
            dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
 
int RMQ(int L,int R)
{
    int k=0;
    while((1<<(k+1))<=R-L+1) k++;
    return min(dp[L][k],dp[R-(1<<k)+1][k]);
}
 
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        scanf("%s",s); memset(sum,0,sizeof sum);
        for(int i=1;i<=n;i++)
        {
            if(s[i-1]=='(') sum[i]=sum[i-1]+1;
            else sum[i]=sum[i-1]-1;
        }
        for(int i=0;i<n;i++) sum[i]=sum[i+1];
 
        memset(dp,0,sizeof dp);
 
        RMQ_init();
 
        for(int i=1;i<=m;i++)
        {
            int a,b; scanf("%d%d",&a,&b);
            if(a>b) swap(a,b);
 
            a--; b--;
            if(s[a]==s[b]) printf("Yes\n");
            else
            {
                if(s[a]=='(')
                {
                    if(RMQ(a,b-1)>=2) printf("Yes\n");
                    else printf("No\n");
                }
 
                else if(s[a]==')') printf("Yes\n");
            }
        }
 
    }
    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值