68分怎么办??

P1241 括号序列

FanHangrui @ 2024-09-18 18:46:19

P1241 68分怎么搞?

求大佬援助!!

代码如下:

#include<bits/stdc++.h>
using namespace std;
stack<int> st;
bool ok[105];
int main(int argc, const char *argv[]) {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    string s;
    cin >> s;
    int len = s.length();
    for(int i = 0, k; i < len; i ++) {
        if (s[i] == ']') {
            if(st.empty()) continue;
            k = st.top();
            if (s[k] == '[') {
                ok[k] = ok[i] = 1;
                st.pop();
            }
        } else if(s[i] == ')') {
            if(st.empty()) continue;
            k = st.top();
            if (s[k] == '(') {
                ok[k] = ok[i] = 1;
                st.pop();
            }
        } else {
            st.push(i);
        }   
    }
    for(int i = 0; i < len; i ++) {
        if(ok[i]) cout << s[i];
        else {
            if (s[i] == '(' || s[i] == ')') printf("()");
            else printf("[]");
        }
    }
    return 0;
}

求指导!!


by wujin2 @ 2024-09-18 18:51:49

@FanHangrui @FanHangrui 看不懂思密达,在下看不懂


by wujin2 @ 2024-09-18 18:53:43

@FanHangrui 可以找hh0592821


by jerry_ao_zx @ 2024-09-18 18:55:16

Hack: ([][][)


by FanHangrui @ 2024-09-18 19:11:33

谢谢jerry_ao_zx的Hack数据

本人已Ac此题

Ac code

#include <bits/stdc++.h>
using namespace std;
const int N = 1e2 + 10;
int a[N];
int main(int argc, const char *argv[]) {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int i, j;
    string s;
    cin >> s;
    int len = s.length();
    for (i = 0; i < len; i ++) {
        if (s[i] == ')') {
            for (j = i - 1; j >= 0; j --) {
                if (s[j] == '(' && a[j] == 0) {
                    a[i] = a[j] = 1;
                    break;
                }
                else if (s[j] == '[' && a[j] == 0) break;
            }

        }
        else if (s[i] == ']') {
            for (j = i - 1; j >= 0; j --) {
                if (s[j] == '[' && a[j] == 0) {
                    a[i] = a[j] = 1;
                    break;
                }
                else if (s[j] == '(' && a[j] == 0) break;
            }
        }
    }
    for (i = 0; i < len; i ++) {
        if (a[i] == 0) {
            if (s[i] == '(' || s[i] == ')') cout << "()";
            else cout << "[]";
        }
        else cout << s[i];
    }
    return 0;
}

|