求助大佬QAQ,蒟蒻只能过4个点
查看原帖
求助大佬QAQ,蒟蒻只能过4个点
967480
jiunichenyin楼主2024/3/23 22:59
#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;
}
2024/3/23 22:59
加载中...