32分求调

P1241 括号序列

hhy8399 @ 2024-07-04 21:26:49

#include<bits/stdc++.h>
using namespace std;

bool pd[10000001];
string s;

int main() {
    cin >> s;
    for (int i = 0; i <= s.size() - 1; i++) {
        if (s[i] == ')') {
            for (int j = i - 1; j >= 0; j--) {
                if (s[j] == '[') {
                    break;
                }
                if (s[j] == '(' && !pd[j]) {
                    pd[i] = 1;
                    pd[j] = 1;
                    break;
                }
            }
        }
        if (s[i] == ']') {
            for (int j = i - 1; j >= 0; j--) {
                if (s[j] == ')') {
                    break;
                }
                if (s[j] == '[' && !pd[j]) {
                    pd[i] = 1;
                    pd[j] = 1;
                    break;
                }
            }
        }
        for (int i = 0; i <= s.size() - 1; i++) {
            if (!pd[i]) {
                if (s[i] == '(' || s[i] == ')') {
                    cout << "()";
                } else if (s[i] == '[' || s[i] == ']') {
                    cout << "[]";
                }
            }
            else cout << s[i];
        }
    }
    return 0;
}

|