神奇的构造题,我的思路比较奇葩。搞了好久,看到WA on 91我绝望了,然后自己造数据,找到了错误,总算是AC了,现在是凌晨0:24分,看到AC之后,感动China!
我写的代码无比的长。。。。。应该有很简单的方法吧。。。。。没想到。
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
#include <stack>
#include <map>
#include <vector>
using namespace std; const int maxn = + ;
char s[maxn];
int a[maxn], b[maxn], sum[maxn], flag[maxn];
int tmpa[maxn], tmpb[maxn], ans[maxn];
int nb[maxn];
int len; void read()
{
scanf("%s", s);
for (int i = ; s[i]; i++) b[i + ] = s[i] - '';
nb[] = b[] * + b[]; for (int i = ; s[i]; i++) nb[i] = s[i] - '';
} void init()
{
len = strlen(s);
memset(flag, , sizeof flag);
} bool check1()
{
for (int i = ; i <= (len + ) / ; i++)
{
if (sum[i] % == ) a[i] = sum[i] / , a[len - i + ] = sum[i] / ;
else a[i] = sum[i] / + , a[len - i + ] = sum[i] - a[i];
} for (int i = ; i <= len; i++) tmpa[i] = a[i];
for (int i = ; i <= len; i++) tmpb[i] = a[len - i + ]; int k = ;
for (int i = ; i <= len; i++)
{
ans[i] = (tmpa[i] + tmpb[i] + k) % ;
k = (tmpa[i] + tmpb[i] + k) / ;
} for (int i = ; i <= len; i++)
if (b[len - i + ] != ans[i]) return ;
return ;
} bool work1()
{
bool fail = ; init(); for (int i = len; i >= len / + ; i--)
{
int id_now = i, id_pre = len - i + ; if (i == len)
{
if (b[i] == ) { fail = ; break; } sum[id_now] = b[id_now]; sum[id_pre] = b[id_now];
flag[id_now] = ; flag[id_pre] = ; if (b[id_now] == b[id_pre]) flag[id_pre + ] = ;
else if (b[id_now] + == b[id_pre]) flag[id_pre + ] = ;
else { fail = ; break; }
} else
{
int num_now, num_pre;
num_now = b[id_now] - flag[id_now + ]; if (num_now == )
{
if (b[id_pre] == )
{
sum[id_now] = ; sum[id_pre] = ;
flag[id_pre + ] = ;
}
else if (b[id_pre] == )
{
sum[id_now] = ; sum[id_pre] = ;
flag[id_pre + ] = ;
}
else { fail = ; break; }
}
else if (b[id_now] == && flag[id_now + ])
{
if (b[id_pre] == || b[id_pre] == )
{
flag[id_now] = ;
if (b[id_pre] == ) flag[id_pre + ] = ;
sum[id_now] = ;
sum[id_pre] = ;
}
else { fail = ; break; }
}
else
{
num_now = num_now + flag[id_pre] * ;
num_pre = b[id_pre] + flag[id_pre] * ; if (num_now == num_pre)
{
flag[id_pre + ] = ;
sum[id_now] = num_now; sum[id_pre] = num_now;
if (num_now >= ) flag[id_now] = ;
} else if (num_now + == num_pre)
{
flag[id_pre + ] = ;
sum[id_now] = num_now; sum[id_pre] = num_now;
if (num_now >= ) flag[id_now] = ;
}
else { fail = ; break; }
}
}
if (id_now == id_pre&&sum[id_pre] % != ) { fail = ; break; }
} if (!check1()) fail = ; return fail;
} bool check2()
{
memset(tmpa, , sizeof tmpa);
memset(tmpb, , sizeof tmpb); for (int i = ; i <= (len + ) / ; i++)
{
if (sum[i] % == ) a[i] = sum[i] / , a[len - i + ] = sum[i] / ;
else a[i] = sum[i] / + , a[len - i + ] = sum[i] - a[i];
} for (int i = ; i <= len; i++) tmpa[i] = a[i];
for (int i = ; i <= len; i++) tmpb[i] = a[len - i + ]; int k = ;
len++;
for (int i = ; i <= len; i++)
{
ans[i] = (tmpa[i] + tmpb[i] + k) % ;
k = (tmpa[i] + tmpb[i] + k) / ;
} for (int i = ; i <= len; i++)
if (b[len - i + ] != ans[i]) return ;
return ;
} bool work2()
{
bool fail = ; init(); len--; for (int i = len; i >= len / + ; i--)
{
int id_now = i, id_pre = len - i + ; if (i == len)
{
sum[id_now] = + nb[id_now]; sum[id_pre] = + nb[id_now];
flag[id_now] = ; flag[id_pre] = ; if (nb[id_now] + == nb[id_pre]) flag[id_pre + ] = ;
else if (nb[id_now] + + == nb[id_pre]) flag[id_pre + ] = ;
else { fail = ; break; }
} else
{
int num_now, num_pre;
num_now = nb[id_now] - flag[id_now + ]; if (num_now == )
{
if (nb[id_pre] == )
{
sum[id_now] = ; sum[id_pre] = ;
flag[id_pre + ] = ;
}
else if (nb[id_pre] == )
{
sum[id_now] = ; sum[id_pre] = ;
flag[id_pre + ] = ;
}
else { fail = ; break; }
}
else if (nb[id_now] == && flag[id_now + ])
{
if (nb[id_pre] == || nb[id_pre] == )
{
flag[id_now] = ;
if (nb[id_pre] == ) flag[id_pre + ] = ;
sum[id_now] = ;
sum[id_pre] = ;
}
else { fail = ; break; }
}
else
{
num_now = num_now + flag[id_pre] * ;
num_pre = nb[id_pre] + flag[id_pre] * ; if (num_now == num_pre)
{
flag[id_pre + ] = ;
sum[id_now] = num_now; sum[id_pre] = num_now;
if (num_now >= ) flag[id_now] = ;
} else if (num_now + == num_pre)
{
flag[id_pre + ] = ;
sum[id_now] = num_now; sum[id_pre] = num_now;
if (num_now >= ) flag[id_now] = ;
}
else { fail = ; break; }
}
}
if (id_now == id_pre&&sum[id_pre] % != ) { fail = ; break; }
} if (!check2()) fail = ; return fail;
} int main()
{
read();
init();
if (!work1())
{
for (int i = ; i <= len; i++) printf("%d", a[i]);
printf("\n");
}
else
{
if (b[] == )
{
if (!work2())
{
for (int i = ; i <= len - ; i++) printf("%d", a[i]);
printf("\n");
}
else { printf("0\n"); }
}
else { printf("0\n"); }
}
return ;
}