P1575 正误问题
做这种题简直就是折磨自己。
第一次见python题解比c++多。
如果您还想在挣扎一下,蒟蒻提供一些数据:戳我哦
化简题目
最重要的是对题目进行化简,将单词变为一个字符。
\[and\quad\Rightarrow\quad\&
\]
\[or\quad\Rightarrow\quad|
\]
\[not\quad\Rightarrow\quad!
\]
\[true\quad\Rightarrow\quad1
\]
\[false\quad\Rightarrow\quad0
\]
判断error
- 
\(\&\) 和 \(|\) 前后不能有 \(\&\) 或 \(|\),前面不能有 \(!\),不能在首位和末尾。 
- 
\(!\) 后面不能有 \(\&\) 或 \(|\),前面不能有 \(1\) 或 \(0\),不能在末位。 
- 
\(1\) 和 \(0\) 前后不能有 \(1\) 或 \(0\),后面不能有 \(!\)。 
计算
按照优先级依次计算即可。
code
//true false or and not
#include<bits/stdc++.h>
using namespace std;
int c;
char s[250],ch[250],cnt;
char c1[5]={'0','t','r','u','e'},c2[6]={'0','f','a','l','s','e'};
char c3[3]={'0','o','r'},c4[4]={'0','a','n','d'},c5[4]={'0','n','o','t'};
bool flag=true;
int find(int now){
	flag=true;for(int i=1;i<=4;i++) if(s[now+i-1]!=c1[i]) flag=false;if(flag) return 1;
	flag=true;for(int i=1;i<=5;i++) if(s[now+i-1]!=c2[i]) flag=false;if(flag) return 2;
	flag=true;for(int i=1;i<=2;i++) if(s[now+i-1]!=c3[i]) flag=false;if(flag) return 3;
	flag=true;for(int i=1;i<=3;i++) if(s[now+i-1]!=c4[i]) flag=false;if(flag) return 4;
	flag=true;for(int i=1;i<=3;i++) if(s[now+i-1]!=c5[i]) flag=false;if(flag) return 5;
}
bool check(){
	for(int i=1;i<=cnt;i++){
		if(ch[i]=='1'){
			if((ch[i-1]=='0'||ch[i-1]=='1')&&i-1!=0) return false;
			if((ch[i+1]=='0'||ch[i+1]=='1')&&i!=cnt) return false;
			if(i==1&&ch[i+1]=='!') return false;
		}
		if(ch[i]=='0'){
			if((ch[i-1]=='0'||ch[i-1]=='1')&&i-1!=0) return false;
			if((ch[i+1]=='0'||ch[i+1]=='1')&&i!=cnt) return false;
			if(i==1&&ch[i+1]=='!') return false;
		}
		if(ch[i]=='|'){
			if((ch[i-1]=='&'||ch[i-1]=='|'||ch[i-1]=='!')&&i-1!=0) return false;
			if((ch[i+1]=='&'||ch[i+1]=='|')&&i!=cnt) return false;
			if(i==1||i==cnt) return false;
		}
		if(ch[i]=='&'){
			if((ch[i-1]=='&'||ch[i-1]=='|'||ch[i-1]=='!')&&i-1!=0) return false;
			if((ch[i+1]=='&'||ch[i+1]=='|')&&i!=cnt) return false;
			if(i==1||i==cnt) return false;
		}
		if(ch[i]=='!'){
			if(ch[i+1]=='&'||ch[i+1]=='|'||i==cnt) return false;
		}
	}
	return true;
}
int ans,fan=0;
char ch1[250],ch2[250],cnt1,cnt2;
void count(){
	//for(int i=1;i<=cnt;i++) cout<<ch[i]<<" ";puts("");
	//处理! 
	for(int i=1;i<=cnt;i++){
		if(ch[i]!='!'){
			if(ch[i]=='1'&&fan) ch[i]='0';
			else if(ch[i]=='0'&&fan) ch[i]='1';
			fan=0;ch1[++cnt1]=ch[i];
		}
		else fan^=1;
	}
	//for(int i=1;i<=cnt1;i++) cout<<ch1[i]<<" ";puts("");
	//处理&  
	for(int i=1;i<=cnt1;i++){
		if(ch1[i]=='|'||((ch1[i]=='1'||ch1[i]=='0')&&ch1[i-1]!='&'&&ch1[i+1]!='&')) ch2[++cnt2]=ch1[i];
		else if(ch1[i]=='&'){
			if(ch1[i-1]=='1'&&ch1[i+1]=='1') ch2[++cnt2]='1';
			else ch2[++cnt2]='0';
		}
	}
	//for(int i=1;i<=cnt2;i++) cout<<ch2[i]<<" ";puts("");
	//处理|
	ans=(ch2[1]=='1')?1:0;
	for(int i=2;i<=cnt2;i++){
		if(ch2[i]=='|'){
			if(ans||ch2[i+1]=='1') ans=1;
			else ans=0;
		}
	} 
}
int main(){
	//思路,先摘成正常字符存起来 
	scanf("%[^\n]%*c",s+1);int len=strlen(s+1);
	//for(int i=len;i>=1;i--) s[i]=s[i-1];
	for(int i=1;i<=len;){//化简
		if(s[i]!=' ') c=find(i);
		if(c==1) ch[++cnt]='1',i+=5;
		if(c==2) ch[++cnt]='0',i+=6;
		if(c==3) ch[++cnt]='|',i+=3;
		if(c==4) ch[++cnt]='&',i+=4;
		if(c==5) ch[++cnt]='!',i+=4;
	}
	if(!check()){//判断error
		puts("error");
		return 0;
	}
	count();//按优先级计算
	if(ans) puts("true");
	else puts("false");
	return 0;
}

 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号