人类智慧题。
思路
通过手玩样例可以发现,每个 _ 上填的东西只与它前后一个数有关。
我们发现这个字符串只由 + 、0 、( 、) 组成。
于是我们可以对每个 _ 前后的字符进行判断,共计 种组合。
在头和尾 _ 需要特判,长度为 也要特判。
具体看代码吧。
code
#include<bits/stdc++.h>
using namespace std;
string t;
int len;
int main(){
cin>>t;
len=t.size();
if(len==1)
{
cout<<0;
return 0;
}
t='#'+t;
for(int i=1;i<=len;i++)
{
if(t[i]=='_')
{
if(i==1&&t[i+1]=='0')
{
t[i]='(';
}
else if(i==1&&t[i+1]=='(')
{
t[i]='(';
}
else if(i==1&&t[i+1]=='+')
{
t[i]='0';
}
// ) 和 “ ” 不可能
else if(i==len&&t[i-1]==')')
{
t[i]=')';
}
else if(i==len&&t[i-1]=='0')
{
t[i]=')';
}
else if(i==len&&t[i-1]=='+')
{
t[i]='0';
}
// ( 和 “ ” 不可能
else if(t[i-1]=='('&&t[i+1]==')')
{
t[i]='0';
}
else if(t[i-1]=='('&&t[i+1]=='(')
{
t[i]='(';
}
else if(t[i-1]=='('&&t[i+1]=='0')
{
t[i]='(';
}
else if(t[i-1]=='('&&t[i+1]=='+')
{
t[i]='0';
}
//________
else if(t[i-1]==')'&&t[i+1]==')')
{
t[i]=')';
}
else if(t[i-1]==')'&&t[i+1]=='(')
{
t[i]='+';
}
else if(t[i-1]==')'&&t[i+1]=='0')
{
t[i]='+';
}
else if(t[i-1]==')'&&t[i+1]=='+')
{
t[i]=')';
}
//________
else if(t[i-1]=='0'&&t[i+1]==')')
{
t[i]=')';
}
else if(t[i-1]=='0'&&t[i+1]=='(')
{
t[i]='+';
}
else if(t[i-1]=='0'&&t[i+1]=='0')
{
t[i]='+';
}
else if(t[i-1]=='0'&&t[i+1]=='+')
{
t[i]=')';
}
//________
else if(t[i-1]=='+'&&t[i+1]==')')
{
t[i]='0';
}
else if(t[i-1]=='+'&&t[i+1]=='(')
{
t[i]='(';
}
else if(t[i-1]=='+'&&t[i+1]=='0')
{
t[i]='(';
}
else if(t[i-1]=='+'&&t[i+1]=='+')
{
t[i]='0';
}
}
}
for(int i=1;i<=len;i++)
{
cout<<t[i];
}
return 0;
}