比赛 |
CSP2022普及组 |
评测结果 |
AAWWWWWWWWTTTTTTTWWW |
题目名称 |
逻辑表达式 |
最终得分 |
10 |
用户昵称 |
┭┮﹏┭┮ |
运行时间 |
9.425 s |
代码语言 |
C++ |
内存使用 |
8.59 MiB |
提交时间 |
2022-10-29 17:20:12 |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 1e6 + 10;
int u = 0,l,s,v[2*N],s1,s2;
char a[2*N],b[2*N];
int sou(int t){
int x;
while(u < l){
if(a[u] == '0' || a[u] == '1'){
x = a[u] - '0';
u++;
}
else if(a[u] == '('){
u++;
}
else if(a[u] == ')'){
u++;
if(a[u] != ')')return x;
}
else if(a[u] == '&'){
bool f = 1;
u++;
int d1,d2;
if(x == 0){
s1++;
d1 = s1,d2 = s2;f = 0;
}
if(a[u] == '(')x &= sou(t+1);
else{
x &= (a[u]-'0');
u++;
}
if(f == 0)s1 = d1,s2 = d2;
}
else{
bool f = 1;
u++;
int d1,d2;
if(x == 1){
s2++;
d2 = s2,d1 = s1;f = 0;
}
if(a[u] == '(')x |= sou(t+1);
else{
x |= (a[u]-'0');
u++;
}
if(f == 0)s2 = d2,s1 = s1;
}
}
return x;
}
void build(){
int le;
while(u < strlen(b)){
if(b[u] == '0' || b[u] == '1'){
if(le != u-1)le = u;
u++;
}
else if(b[u] == '('){
le = u;
u++;
}
else if(b[u] == ')'){
u++;
return;
}
else if(b[u] == '&'){
u++;
if(b[u] == '('){
build();
v[le] = 1,v[u-1] = 2;
}
else{
v[le] = 1,v[u] = 2;
u++;
}
if(b[u] != ')'){
le = u-1;
}
}
else{
u++;
if(b[u] == '(')build();
else{
u++;
}
if(b[u] != ')'){
le = u-1;
}
}
}
}
int main(){
freopen("csp2022pj_expr.in","r",stdin);
freopen("csp2022pj_expr.out","w",stdout);
cin>>b;
build();
for(int i = 0;i < strlen(b);i++){
if(v[i] == 0){
a[i+s] = b[i];
}
else if(v[i] == 1){
a[i+s] = '(';s++;
a[i+s] = b[i];
}
else if(v[i] == 2){
a[i+s] = b[i];s++;
a[i+s] = ')';
}
}
l = strlen(a);
u = 0;
cout<<sou(0)<<endl;
cout<<s1<<' '<<s2<<endl;
return 0;
}