求助大佬QAQ,蒟蒻只能过4个点

P1241 括号序列

jiunichenyin @ 2024-03-23 22:59:41

#include<iostream>
#include<iomanip>
#include<math.h>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<vector>
#include<stack>
#define maxn 1000010
using namespace std;
queue<char> q;
struct node {
    char s;
    bool pan;
}z[maxn];//标记并储存字符
int sum,point = 0;
int main() {
    char a;
    while (cin >> a) {
        q.push(a);
    }
    while (q.size()>0) {
        char one = q.front();
        if (one == '(') {
            z[point].s = one;
            z[point].pan = false;
            point++;
            q.pop();
        }
        else if (one == ')') {
            for (int i = point-1; i >=0; --i) {
                if (z[i].s == '(') {
                    if (z[i].pan == false) {
                        z[i].pan = true;
                        z[point].s = one;
                        z[point].pan = true;
                        point++;
                        q.pop();
                        break;
                    }
                    else if (z[i].pan == true) {
                        z[point].s = one;
                        z[point].pan = false;
                        point++;
                        q.pop();
                        break;
                    }
                }
                else if (z[i].s == '[') {
                        z[point].s = one;
                        z[point].pan = false;
                        point++;
                        q.pop();
                        break;
                }
            }
        }
        else if (one == ']') {
            for (int i = point - 1; i >= 0; --i) {
                if (z[i].s == '[') {
                    if (z[i].pan == false) {
                        z[i].pan = true;
                        z[point].s = one;
                        z[point].pan = true;
                        point++;
                        q.pop();
                        break;
                    }
                    else if (z[i].pan == true) {
                        z[point].s = one;
                        z[point].pan = false;
                        point++;
                        q.pop();
                        break;
                    }
                }
                else if (z[i].s == '(') {
                        z[point].s = one;
                        z[point].pan = false;
                        point++;
                        q.pop();
                        break;
                }
            }
        }
        else if (one == '[') {
            z[point].s = one;
            z[point].pan = false;
            point++;
            q.pop();
        }
        //cout << z[point-1].s << " " << z[point-1].pan << endl;
    }//对能配对的进行标记并储存进结构体
    for (int i = 0; i < point; ++i) {
        if (z[i].pan) {
            cout << z[i].s;
        }
        else{
            if (z[i].s == '('||z[i].s==')') {
                cout << "()";
            }
            else if (z[i].s == '[' || z[i].s == ']') {
                cout << "[]";
            }
        }
    }//输出
    return 0;
}

by jiunichenyin @ 2024-03-24 10:36:07

代码改成这样后有6个点超时,大佬帮帮QAQ

#include<iostream>
#include<iomanip>
#include<math.h>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<vector>
#include<stack>
#define maxn 1000010
using namespace std;
queue<char> q;
struct node {
    char s;
    bool pan;
}z[maxn];
int sum, point = 0;
int main() {
    char a;
    while (cin >> a) {
        q.push(a);
    }
    while (q.size() > 0) {
        char one = q.front();
        if (one == '(') {
            z[point].s = one;
            z[point].pan = false;
            point++;
            q.pop();
        }
        else if (one == ')') {
            for (int i = point - 1; i >= 0; --i) {
                if (z[i].s == '('&&z[i].pan==false) {
                    if (z[i].pan == false) {
                        z[i].pan = true;
                        z[point].s = one;
                        z[point].pan = true;
                        point++;
                        q.pop();
                        break;
                    }
                    else if (z[i].pan == true) {
                        z[point].s = one;
                        z[point].pan = false;
                        point++;
                        q.pop();
                        break;
                    }
                }
                else if (z[i].s == '['&&z[i].pan==false) {
                    z[point].s = one;
                    z[point].pan = false;
                    point++;
                    q.pop();
                    break;
                }
            }
        }
        else if (one == ']') {
            for (int i = point - 1; i >= 0; --i) {
                if (z[i].s == '[' && z[i].pan == false) {
                    if (z[i].pan == false) {
                        z[i].pan = true;
                        z[point].s = one;
                        z[point].pan = true;
                        point++;
                        q.pop();
                        break;
                    }
                    else if (z[i].pan == true) {
                        z[point].s = one;
                        z[point].pan = false;
                        point++;
                        q.pop();
                        break;
                    }
                }
                else if (z[i].s == '(' && z[i].pan == false) {
                    z[point].s = one;
                    z[point].pan = false;
                    point++;
                    q.pop();
                    break;
                }
            }
        }
        else if (one == '[') {
            z[point].s = one;
            z[point].pan = false;
            point++;
            q.pop();
        }
        //cout << z[point-1].s << " " << z[point-1].pan << endl;
    }
    for (int i = 0; i < point; ++i) {
        if (z[i].pan) {
            cout << z[i].s;
        }
        else {
            if (z[i].s == '(' || z[i].s == ')') {
                cout << "()";
            }
            else if (z[i].s == '[' || z[i].s == ']') {
                cout << "[]";
            }
        }
    }
    return 0;
}

by 123huchenghao @ 2024-06-28 20:39:41

#include<iostream>
#include<stack>
using namespace std;
int main()
{
    stack<int> stk;
    string str;
    cin >> str;
    int len = str.length();
    int f[101] = { 0 };
    for (int i = 0; i < len; i++)
    {
        if (str[i] == ')')
        {
            if (!stk.empty() && str[stk.top()] == '(')
            {
                f[stk.top()] = f[i] = 1;
                stk.pop();
            }
        }
        else if (str[i] == ']')
        {
            if (!stk.empty() && str[stk.top()] == '[')
            {
                f[stk.top()] = f[i] = 1;
                stk.pop();
            }
        }
        else stk.push(i);
    }
    for (int i = 0; i < len; i++)
    {
        if (f[i])
        {
            cout << str[i];
        }
        else
        {
            if (str[i] == ']' || str[i] == '[')cout << "[]";
            if (str[i] == ')' || str[i] == '(')cout << "()";
        }
    }
    return 0;
}

|