蒟蒻6个点RE了

P1241 括号序列

nibuxvyaozhidao @ 2024-05-10 16:28:08

代码:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    stack<pair<char,int>> stk;
    string a;
    cin>>a;
    for(int i=0;i<a.length();i++){
        if(a[i]=='('||a[i]=='['){
            stk.push(make_pair(a[i],i));
        }else{
            if(a[i]==']'&&stk.top().first=='['){
                stk.pop();
            }else if(a[i]==')'&&stk.top().first=='('){
                stk.pop();
            }else{
                a.insert(i,((a[i]==')')?"(":"["));
                i++;
            }
        }
    }
    while(!stk.empty()){
        auto tmp=stk.top();
        stk.pop();
        if(tmp.first=='['){
            a.insert(tmp.second+1,"]");
        }else{
            a.insert(tmp.second+1,")");
        }
    }
    cout<<a;
    return 0;
}


by nibuxvyaozhidao @ 2024-05-10 16:32:50

改了之后还有3个点RE 代码:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    stack<pair<char,int>> stk;
    string a;
    cin>>a;
    for(int i=0;i<a.length();i++){
        if(a[i]=='('||a[i]=='['){
            stk.push(make_pair(a[i],i));
        }else{
            if(a[i]==']'&&!stk.empty()&&stk.top().first=='['){
                stk.pop();
            }else if(a[i]==')'&&stk.top().first=='('){
                stk.pop();
            }else{
                a.insert(i,((a[i]==')')?"(":"["));
                i++;
            }
        }
    }
    while(!stk.empty()){
        auto tmp=stk.top();
        stk.pop();
        if(tmp.first=='['){
            a.insert(tmp.second+1,"]");
        }else{
            a.insert(tmp.second+1,")");
        }
    }
    cout<<a;
    return 0;
}

by nibuxvyaozhidao @ 2024-05-10 16:34:03

淦,我自己纸张了,没给小括号加stk是否为空


by luozezhong @ 2024-05-13 10:55:27

![why!!!](file:///C:/Users/Administrator/Desktop/%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE%202024-05-10%20162635.png)

???。。。


|