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;
}