Xmas Contest 2024 B 题解

· · 题解

solution

不难发现,两边的字符确认后,中间的字符的选择是唯一的。

具体的说,设当前是第 i 个字符且 i 是奇数,那么:

:::info[若第 i-1 个字符是 ( 或者 +]{open}

:::info[若第 i-1 个字符是 0 或者 )]{open}

对于第一个和最后一个字符,进行特判。

并且注意特判只有一个 _ 的情况,输出 0。

code

#include<bits/stdc++.h>
#define For(i,a,b) for(int i = a;i<=b;i++)
using namespace std;
int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    string t;
    cin>>t;
    if(t == "_"){
        cout<<"0";
        return 0;
    }
    int len = t.size()-1;
    if(t[1] == '+') t[0] = '0';
    else t[0] = '(';
    if(t[len-1] == '+') t[len] = '0';
    else t[len] = ')';
    For(i,1,len-1){
        if(t[i] == '_'){
            if(t[i-1] == '(' || t[i-1] == '+'){
                if(t[i+1] == '(' || t[i+1] == '0'){
                    t[i] = '(';
                }
                else t[i] = '0';
            }
            else{
                if(t[i+1] == '+' || t[i+1] == ')') t[i] = ')';
                else t[i] = '+';
            }
        }
    }
    cout<<t;
    return 0;
}