50求调(递归与栈的随意缝合)
查看原帖
50求调(递归与栈的随意缝合)
1440137
fanzihao0327楼主2024/10/7 20:49

昨天直接解锁四色了

#include <bits/stdc++.h>
using namespace std;
int break1=0,break2=0;
char ans;
string s;

int find1 (int i) {
	int t=0;
	do{
		if (s[i]=='(') t++;
		if (s[i]==')') t--;
		i++;
	}while (t!=0);
	return i-1;
}

int find (int i) {
	i++;
	if(s[i]=='('){
	    i=find1 (i);
	}else{
		for(;s[i]!='|'&&s[i]!=')'&&i!=s.length();i++);
		i--;
	}
	return i;
} 

int find2 (int i) {
	i++;
	if(s[i]=='('){
	    i=find1 (i);
	}
	return i;
}

char calculate (int l,int r) {
	stack <char> S;
	S.push ('#');
	for(int i=l;i<=r;i++){
		char a=s[i];
		if(a=='&'){
			if (S.top()=='0') {
			    break1++;
				i=find2 (i);
				continue;
			}
			if (S.top()=='1') {
				char b;
				if (s[i+1]=='(') {
					b=calculate (i+2,find1(i+1)-1);
					i=find1(i+1);
				}else {
					b=s[i+1];
					i++;
				}
				if (S.top()!=b){
					S.pop();
					S.push('0');
				}
				continue;
			}
		}
		if(a=='|'){
			if (S.top()=='1') {
		    	break2++;
				i=find (i);
				continue;
			}
			if (S.top()=='0') {
			    char b;
			   	b=calculate (i+1,find1(i+1)-1);
			    i=find1(i+1)-1;
			    if (a!=b){
				    S.pop();
				    S.push('1');
			    }
			    continue;
			}
		}
		if(a=='('){
			S.push(calculate (i+1,find1(i)-1));
			i=find1(i);
			continue;
		}
		S.push(a); 
	}
	for(;;){
	if(S.top()==')') S.pop();
	else return S.top();
	}
}

int main () {
	cin>>s;
	ans=calculate (0,s.length()-1);
	cout<<ans<<endl<<break1<<' '<<break2;
	return 0;
}
2024/10/7 20:49
加载中...