首页 > 其他 > 详细

UVALive 4882 Parenthesis 删除不必要的括号 模拟题

时间:2014-09-06 01:08:22      阅读:352      评论:0      收藏:0      [点我收藏+]

题目链接:点击打开链接

题意:给定一个合法的序列,删掉所有不必要的括号。


#include <cstdio>
#include <cstring>

const int MAX_N = 10007;

char a[MAX_N];
int stack[MAX_N], top;
bool mark[MAX_N], stacknow[MAX_N];

int main() {
	while (1 == scanf("%s", a)) {
		int len = (int) strlen(a);
		top = 0;
		memset(mark, false, sizeof mark);
		for (int i = 0; i < len; ++i) {
			if (a[i] == '(') {
				stack[top++] = i;
				stacknow[top - 1] = false;
			} else if (a[i] == '+') {
				if (top) {
					stacknow[top - 1] = true;
				}
			} else if(a[i] == ')') {
				if ((stacknow[top - 1] && (stack[top - 1] - 1 >= 0 && a[stack[top - 1] - 1] != '+' && a[stack[top - 1] - 1] != '(' ||
				i + 1 < len && a[i + 1] != '+' && a[i + 1] != ')')) == 0) {
					if (stack[top - 1] - 1 >= 0 && a[stack[top - 1] - 1] == '(' && i + 1 < len && a[i + 1] == ')') 
						stacknow[top - 2] = stacknow[top - 1];
					mark[stack[top - 1]] = mark[i] = true;
				 }
			 	--top;
			}
		}
		for (int i = 0; i < len; ++i) {
			if (!mark[i]) putchar(a[i]);
	 	}
	 	puts("");
	}
	return 0;
}


UVALive 4882 Parenthesis 删除不必要的括号 模拟题

原文:http://blog.csdn.net/qq574857122/article/details/39089759

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!