(Your)((Term)((Project)))(字符串处理)
来源:互联网 发布:js 返回n天后的日期 编辑:程序博客网 时间:2024/06/02 13:44
(Your)((Term)((Project)))
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 2709 Accepted: 1010
Description
You have typed the report of your term project in your personal computer. There are several one line arithmetic expressions in your report. There is no redundant parentheses in the expressions (omitting a pair of redundant matching parentheses does not change the value of the expression). In your absence, your little brother inserts some redundant matching parentheses in the expressions of your report. Assume that the expressions remain syntactically correct and evaluate to their original value (the value before inserting redundant parentheses). To restore your report to its original form, you are to write a program to omit all redundant parentheses.
To make life easier, consider the following simplifying assumptions:
Note that the only transformation allowed is omission of redundant parentheses, and no algebraic simplification is allowed.
To make life easier, consider the following simplifying assumptions:
- The input file contains a number of expressions, each in one separate line.
- Variables in the expressions are only single uppercase letters.
- Operators in the expressions are only binary '+' and binary '-'.
Note that the only transformation allowed is omission of redundant parentheses, and no algebraic simplification is allowed.
Input
The input consists of several test cases. The first line of the file contains a single number M, which is the number of test cases (1 <= M <= 10). Each of the following M lines, is exactly one correct expression. There may be arbitrarily space characters in each line. The length of each line (including spaces) is at most 255 characters.
Output
The output for each test case is the same expression without redundant parentheses. Notice that the order of operands in an input expression and its corresponding output should be the same. Each output expression must be on a separate line. Space characters should be omitted in the output expressions.
Sample Input
3(A-B + C) - (A+(B - C)) - (C-(D- E) ) ((A)-( (B)))A-(B+C)
Sample Output
A-B+C-(A+B-C)-(C-(D-E))A-BA-(B+C)
Source
Tehran 1999
[Submit] [Go Back] [Status] [Discuss]
/*Source CodeProblem: 1690Memory: 184KTime: 0MSLanguage: C++Result: Accepted*/ #include <iostream> #include <cstdio> using namespace std; char s[300], d[300]; void func(int i) //去掉')' { int cnt = 0; for(i++; s[i] != '\0'; i++) { if(s[i] == '(') cnt++; else if(s[i] == ')') { if(!cnt) { s[i] = ' '; return ; } cnt--; } } } int main() { int n, i, j; scanf("%d", &n); getchar(); while(n--) { gets(s); for(i = 0, j = -1; s[i] != '\0'; i++) { if(s[i] == ' ') continue; else if(s[i] == '(') { if(j == -1 || d[j] == '+' || d[j] == '(') func(i); else if(d[j] == '-') { int k, cnt = 0, tag = 0; for(k = i + 1; s[k] != '\0'; k++) { if(s[k] == '(') cnt++; else if(s[k] == ')') { if(!cnt) break; cnt--; } else if(s[k] == '+' || s[k] == '-') { if(!cnt) { tag = 1; break; } } } if(tag) d[++j] = s[i]; else func(i); } } else d[++j] = s[i]; } d[++j] = '\0'; printf("%s\n", d); } return 0; }
- (Your)((Term)((Project)))(字符串处理)
- TOJ 2196 ZOJ 1423 (Your)((Term)((Project))) 字符串处理
- POJ--1690 (Your)((Term)((Project)))(字符串处理)
- POJ1690 (Your)((Term)((Project)))
- zoj1423-(Your)((Term)((Project)))
- (Your)((Term)((Project)))
- poj 1690 (Your)((Term)((Project)))
- zoj 1423 (Your)((Term)((Project)))
- POJ 1690 - (Your)((Term)((Project)))
- poj 1690 (Your)((Term)((Project)))
- 1690 (Your)((Term)((Project))) 解题报告
- ZOJ 1423 (Your)((Term)((Project))) (POJ 1690)
- ZOJ Problem Set - 1423 (Your)((Term)((Project)))
- ZOJ 1423_(Your)((Term)((Project)))
- ZOJ 1423 POJ 1690 (Your)((Term)((Project)))
- POJ 1690 (Your)((Term)((Project))) 水题
- poj 模拟 - 1690 (Your)((Term)((Project)))
- ZOJ-1423-(Your)((Term)((Project))) 【模拟】
- Java面试宝典2013版(超长版)
- JAVA 动态加载properties配置文件
- final关键字
- Linux下FTP操作命令详解
- struts2 jsp页面向action提交list对象
- (Your)((Term)((Project)))(字符串处理)
- ECLIPSE下部署tomcat时遇见的一些问题
- CTex + Texmaker
- 搭建Web服务器之Step6:CentOS6.3安装JDK和环境配置
- 怎么查询局域网内全部电脑IP和mac地址等信息?
- Leetcode: Decode ways
- 16 个 Linux 服务器监控命令和watch
- 从数据库sql数据到gridview然后导成excel
- Android 开发源码分享