最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

P8815-CSP-J-2022-3-LogicExpression邏輯表達(dá)式

2023-08-07 11:17 作者:信奧賽USACO鄭老師  | 我要投稿

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAXV=1e6;

struct node{

? ? char v;

? ? int l,r;

};

vector<node> g(MAXV);

int build_tree(string sl){

? ? int last=1;

? ? stack<int> st;

? ? for(int i=0;i<sl.size();i++){

? ? ? ? if(sl[i]=='0'||sl[i]=='1'){

? ? ? ? ? ? g[last].v=sl[i];

? ? ? ? ? ? st.push(last);

? ? ? ? ? ? last++;

? ? ? ? }

? ? ? ? if(sl[i]=='&'||sl[i]=='|'){

? ? ? ? ? ? int o2=st.top();

? ? ? ? ? ? st.pop();

? ? ? ? ? ? int o1=st.top();

? ? ? ? ? ? st.pop();

? ? ? ? ? ? g[last].l=o1;

? ? ? ? ? ? g[last].r=o2;

? ? ? ? ? ? g[last].v=sl[i];

? ? ? ? ? ? st.push(last);

? ? ? ? ? ? last++;

? ? ? ? }

? ? }

? ? return st.top();

}

int dfs(int root, int& a, int& b){

? ? if(g[root].l==0&&g[root].r==0){//leaf

? ? ? ? return g[root].v-'0';

? ? }

? ? int left=dfs(g[root].l,a,b);

? ? int ret=-1;

? ? if(g[root].v=='&'){

? ? ? ? if(left==0){

? ? ? ? ? ? a++;

? ? ? ? ? ? ret=0;

? ? ? ? }else{

? ? ? ? ? ? ret=dfs(g[root].r,a,b);

? ? ? ? }

? ? }else{

? ? ? ? if(g[root].v=='|'){

? ? ? ? ? ? if(left==1){

? ? ? ? ? ? ? ? b++;

? ? ? ? ? ? ? ? ret=1;

? ? ? ? ? ? }else{

? ? ? ? ? ? ? ? ret=dfs(g[root].r,a,b);

? ? ? ? ? ? }

? ? ? ? }

? ? }

? ? return ret;

}

string m2l(string s){

? ? string res;

? ? stack<char> st;

? ? for(int i=0;i<s.size();i++){

? ? ? ? char c=s[i];

? ? ? ? switch(c){

? ? ? ? ? ? case '0':

? ? ? ? ? ? case '1':

? ? ? ? ? ? ? ? res+=c;

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case '(':

? ? ? ? ? ? ? ? st.push(c);

? ? ? ? ? ? ? ? break;? ??

? ? ? ? ? ? case ')':

? ? ? ? ? ? ? ? while(st.top()!='('){

? ? ? ? ? ? ? ? ? ? res+=st.top();

? ? ? ? ? ? ? ? ? ? st.pop();

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? st.pop();

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case '&':

? ? ? ? ? ? ? ? while(st.size()>0&&st.top()!='|'&&st.top()!='('){

? ? ? ? ? ? ? ? ? ? res+=st.top();

? ? ? ? ? ? ? ? ? ? st.pop();

? ? ? ? ? ? ? ? }? ??

? ? ? ? ? ? ? ? st.push('&');

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case '|':

? ? ? ? ? ? ? ? while(st.size()>0&&st.top()!='('){

? ? ? ? ? ? ? ? ? ? res+=st.top();

? ? ? ? ? ? ? ? ? ? st.pop();

? ? ? ? ? ? ? ? }? ??

? ? ? ? ? ? ? ? st.push('|');

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? default:

? ? ? ? ? ? ? ? cout<<"error1\n";

? ? ? ? }

? ? }

? ? while(st.size()>0){

? ? ? ? res+=st.top();

? ? ? ? st.pop();

? ? }

? ? return res;? ? ? ? ? ? ? ? ? ??

}

int main(){

? ? string s,sl;

? ? cin>>s;

? ? sl=m2l(s);

? ? int root=build_tree(sl);

? ? int a=0,b=0;

? ? cout<<dfs(root,a,b)<<endl;

? ? cout<<a<<" "<<b<<endl;

? ? return 0;

}


P8815-CSP-J-2022-3-LogicExpression邏輯表達(dá)式的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
鹤庆县| 河源市| 泾阳县| 保山市| 威海市| 涪陵区| 泽州县| 新晃| 康平县| 永宁县| 新乐市| 清新县| 岱山县| 礼泉县| 监利县| 香港 | 阿鲁科尔沁旗| 台前县| 义乌市| 玉树县| 博湖县| 红桥区| 广西| 房产| 普定县| 军事| 宜兴市| 全南县| 安乡县| 嘉祥县| 锦州市| 南城县| 江源县| 栖霞市| 沾化县| 阜平县| 彰武县| 哈尔滨市| 枣强县| 喀喇沁旗| 历史|