(Your)((Term)((Project)))(字符串处理)
2013-01-16 23:15
232 查看
[align=center](Your)((Term)((Project)))[/align]
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:
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
Sample Output
Source
Tehran 1999
[Submit] [Go Back] [Status] [Discuss]
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 2709 | Accepted: 1010 |
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:
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-B A-(B+C)
Source
Tehran 1999
[Submit] [Go Back] [Status] [Discuss]
/* Source Code Problem: 1690 Memory: 184K Time: 0MS Language: 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; }
相关文章推荐
- TOJ 2196 ZOJ 1423 (Your)((Term)((Project))) 字符串处理
- (Your)((Term)((Project)))(字符串处理)
- POJ--1690 (Your)((Term)((Project)))(字符串处理)
- POJ--1690 (Your)((Term)((Project)))(字符串处理)
- ZOJ Problem Set - 1423 (Your)((Term)((Project)))
- POJ 1690 (Your)((Term)((Project)))
- POJ 1690 (Your)((Term)((Project)))
- (Your)((Term)((Project)))
- your project contains error(s) please fix them before running your application的处理方法
- Eclipse中“Your project contains error(s)”处理方法
- 4000 poj1690 (Your)((Term)((Project))) (括号配对)
- [USACO] Section1.1 Your Ride Is Here[字符串处理]
- ZOJ 1423 POJ 1690 (Your)((Term)((Project)))
- 【USACO-Chapter1-1.1】【字符串处理】Your Ride Is Here
- [USACO] Section1.1 Your Ride Is Here[字符串处理]
- zoj1423(Your)((Term)((Project))) (栈——基础练习)
- poj 1690 (Your)((Term)((Project)))
- zoj 1423 (Your)((Term)((Project)))
- zoj1423(Your)((Term)((Project)))
- 1690 (Your)((Term)((Project))) 解题报告