分类讨论就行了。。。。想要搞成0的话最后一位一定要是0,然后前面有00,01,10,11四种情况,每种情况都想一下就行了。。。
#include <iostream> #include <queue> #include <stack> #include <map> #include <set> #include <bitset> #include <cstdio> #include <algorithm> #include <cstring> #include <climits> #include <cstdlib> #include <cmath> #include <time.h> #define maxn 100005 #define maxm 100005 #define eps 1e-12 #define mod 1000003 #define INF 0x3f3f3f3f #define PI (acos(-1.0)) #define lowbit(x) (x&(-x)) #define mp make_pair #define ls o<<1 #define rs o<<1 | 1 #define lson o<<1, L, mid #define rson o<<1 | 1, mid+1, R #pragma comment(linker, "/STACK:102400000,102400000") #define pii pair<int, int> typedef long long LL; typedef unsigned long long ULL; //typedef int LL; using namespace std; LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;} LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;} // head int a[maxn]; char s[maxn]; void work() { int n; scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%d", &a[i]); if(n == 1) { if(a[1] == 0) printf("YES\n0\n"); else printf("NO\n"); return; } if(a[n] == 1) { printf("NO\n"); return; } if(a[n-1] == 1) { printf("YES\n"); for(int i = 1; i <= n; i++) printf("%d%s", a[i], i == n ? "\n" : "->"); } else { if(n == 2) printf("NO\n"); else if(a[n-2] == 0) { printf("YES\n"); for(int i = 1; i <= n-3; i++) printf("%d%s", a[i], i == n ? "\n" : "->"); printf("(0->0)->0\n"); } else { int ok = 0, pos; for(int i = n-2; i >= 1; i--) if(a[i] == 0) { ok = 1; pos = i; break; } if(!ok) printf("NO\n"); else { printf("YES\n"); for(int i = 1; i < pos; i++) printf("%d->", a[i]); printf("(0->("); for(int i = pos+1; i <= n-2; i++) printf("%d->", a[i]); printf("0))->0\n"); } } } } int main() { work(); return 0; }