Codeforces Round #306 (Div. 2) E - Brackets in Implications 构造

E - Brackets in Implications 题意 :给出 1->1 = 1, 0->1(1) = 1, 1->0 = 0。给出n个数字(0或1),加括号问能不能使表达式最终为0.

想办法使n个数分为4个部分  x ->( 0 -> y ) -> 0,(x,y  = 0, 1)即可。详见代码

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define ls rt << 1
#define rs rt << 1 | 1
#define pi acos(-1.0)
#define eps 1e-8
#define asd puts("sdfsdfsdfsdfsdfsdf");
typedef __int64 ll;
typedef pair<int, int> pll;
const int N = 100010;

char a[N];

int main(){
    int n;
    while( ~scanf("%d%*c", &n) ) {
        for(int i = 0; i < n; ++i )
            scanf("%c%*c", &a[i]);
        if( n == 1 ) {
            if( a[0] == '0' )
                printf("YES\n0\n");
            else
                printf("NO\n");
            continue;
        }
        if( n == 2 ) {
            if( a[0] == '1' && a[1] == '0' )
                printf("YES\n1->0\n");
            else
                printf("NO\n");
            continue;
        }
        if( a[n-1] == '1' ) {
            printf("NO\n");
            continue;
        }
        if( a[n-2] == '1' ) {
            //******->1->0 ---> 1->0 ----- 0
            printf("YES\n");
            for(int i = 0; i < n-1; ++i )
                printf("%c->", a[i]);
            printf("%c", a[n-1]);
            printf("\n");
            continue;
        }
        // **** -> (0 -> (***)) -> 0
        //          0->* = 1
        int l0 = -1 , r0 = n-2;
        for( int i = n-3; i >= 0; --i ) {
            if( a[i] == '0' ) {
                l0 = i;
                break;
            }
        }
        if( l0 == -1 ) {
            puts("NO");
            continue;
        }
        puts("YES");
        for( int i = 0; i < l0; ++i )
            printf("%c->", a[i]);
        for( int i = l0; i < r0; ++i )
            printf("(%c->", a[i]);
        printf("%c", a[n-2]);
        for( int i = l0; i < r0; ++i )
            printf(")");
        printf("->%c", a[n-1]);
        puts("");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值