设为首页 加入收藏

TOP

UVALive 4882 Parenthesis 删除不必要的括号 模拟题
2015-07-20 17:45:14 来源: 作者: 【 】 浏览:1
Tags:UVALive 4882 Parenthesis 删除 必要 括号 模拟题

题目链接:点击打开链接

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


#include 
  
   
#include 
   
     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; }
   
  


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇POJ 1149 PIGS ,最大流 下一篇UVALive 4885 Task 差分约束

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Shell 传递参数 (2025-12-25 00:50:45)
·Linux echo 命令 - (2025-12-25 00:50:43)
·Linux常用命令60条( (2025-12-25 00:50:40)
·nginx 监听一个端口 (2025-12-25 00:19:30)
·整个互联网就没有一 (2025-12-25 00:19:27)