【C++】95pts,WA #20
查看原帖
【C++】95pts,WA #20
567610
RDFZchenyy楼主2022/5/4 14:24
#include<bits/stdc++.h>
using namespace std;

#define MAXN 1000009
typedef long long LL;

int n;
char s1[MAXN],s2[MAXN]; 
int pp1[MAXN],pp2[MAXN];
stack<int> pps;

LL pb(int l,int r,int pp[],int fl){//保证l与r匹配,l~r为"(...)" 
	if(l+1==r){
		return 0; 
	}
	if(pp[r-1]==l+1){//内层匹配
		if(fl){
			return pb(l+1,r-1,pp,1);
		}else{
			return pb(l+1,r-1,pp,1)+1;
		}
	}
	LL res=2-fl;
	for(int i=l+1;i<=r-1;i=pp[i]+1){
		res+=pb(i,pp[i],pp,0);
	}
	return res;
}

LL calc(int l,int r){
	if(l+1==r){//即括号序列"()" 
		return 0;
	}
	LL res=0;
	
	for(int i=l;i<=r;i=pp1[i]+1){
		if(pp1[i]==pp2[i]){//s1和s2可以匹配的"(...)" 		
			res+=calc(i+1,pp1[i]-1);
		}else{//即s1的"(...)"需要拍扁操作 
			res+=pb(i,pp1[i],pp1,0);
		}
	}
	for(int i=l;i<=r;i=pp2[i]+1){
		if(pp1[i]!=pp2[i]){//即s2的"(...)"需要拍扁操作 
			res+=pb(i,pp2[i],pp2,0);
		}
	}
	return res;
}

int main(){
	scanf("%d",&n);
	scanf("%s",s1+1);
	scanf("%s",s2+1);
	for(int i=1;i<=n;i++){
		if(s1[i]=='('){
			pps.push(i);
		}else{
			pp1[i]=pps.top();
			pp1[pp1[i]]=i;
			pps.pop();
		}
	}
	for(int i=1;i<=n;i++){
		if(s2[i]=='('){
			pps.push(i);
		}else{
			pp2[i]=pps.top();
			pp2[pp2[i]]=i;
			pps.pop();
		}
	}
	if(s1==s2){
		printf("0\n");
	}else{
		printf("%lld",calc(1,n));
	}	

	return 0;
}
2022/5/4 14:24
加载中...