当前位置:网站首页>Identify some positions in the parenthesis sequence

Identify some positions in the parenthesis sequence

2022-07-23 20:51:00 Park Xiaoming

link :C. Recover an RBS

The question :
Given the bracket sequence , Some of these locations are unknown , If these unknown locations can determine what to fill , It would be YES, otherwise NO.

practice :
cnt Statistical question mark ? The number of ,sum Is the number of left parentheses - Number of right parentheses . When sum + cnt == 1 when , It can be determined that cnt individual ? All for (. At this time, you just need to empty cnt, And then let sum = 1.
Last , If cnt == sum, Explain the final remaining cnt individual ? It has to be for ), That is, you can output YES 了 .

Code :

void solve()
{
    
    string s;
    cin >> s;
    int cnt = 0, sum = 0;
    for (auto i : s)
    {
    
        if (i == '(') sum++;
        else if (i == ')') sum--;
        else cnt++;
        if (cnt + sum == 1)
        {
    
            cnt = 0;
            sum = 1;
        }
    }
    if (sum == cnt) cout << "YES\n";
    else cout << "NO\n";
}
原网站

版权声明
本文为[Park Xiaoming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207232041096202.html