pku 1690 (Your)((Term)((Project))) (模拟题)
2013-07-04 17:27
260 查看
(Your)((Term)((Project)))
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
题意:讲式子的空格和多余的括号去掉
题解:由于是模拟题,数据很小,直接暴力就好了。先扫描一次将空格去掉,再不断扫描,将(A)类型的括号去掉,直到不能去为止。再扫描一次,将所有括号用match数组匹配起来,若 s[i]==')' 那么 match[i]为与它匹配的'('所在的下标。再扫描一次,将'('前面不为负号的括号对全部标记mark[i]=1表示可以去掉。最后扫描一次,根据mark[i]数组将多余括号去掉~~~
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 2830 | Accepted: 1064 |
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
题意:讲式子的空格和多余的括号去掉
题解:由于是模拟题,数据很小,直接暴力就好了。先扫描一次将空格去掉,再不断扫描,将(A)类型的括号去掉,直到不能去为止。再扫描一次,将所有括号用match数组匹配起来,若 s[i]==')' 那么 match[i]为与它匹配的'('所在的下标。再扫描一次,将'('前面不为负号的括号对全部标记mark[i]=1表示可以去掉。最后扫描一次,根据mark[i]数组将多余括号去掉~~~
#include<stdio.h> #include<string.h> char s[258]; int mark[258],match[258],zhan[258],num,opnum; int main() { int m,i,j,l,temp; scanf("%d",&m); getchar(); while(m--) { gets(s); memset(mark,-1,sizeof(mark)); memset(match,-1,sizeof(match)); for(l=strlen(s),i=j=0;i<l;i++) { if(s[i]==' ') continue; s[j++]=s[i]; } l=j,s[j]='\0',temp=1; while(temp) { for(temp=j=i=0;i<l;i++) { if(s[i]=='('&&s[i+2]==')') s[j++]=s[i+1],i=i+2,temp=1; else s[j++]=s[i]; } l=j,s[j]='\0'; } for(num=i=0;i<l;i++) { if(s[i]=='(') match[i]=i,zhan[num++]=i; else if(s[i]==')') num--,match[i]=zhan[num]; } for(i=0,temp=1;i<l;i++) { if(s[i]=='('&&temp==1) mark[i]=1; else if(s[i]==')'&&mark[match[i]]==1) mark[i]=1; if(s[i]=='-') temp=0; else temp=1; } for(i=j=0;i<l;i++) { if(s[i]=='('&&mark[i]==1) continue; else if(s[i]==')'&&mark[i]==1) continue; s[j++]=s[i]; } s[j]='\0'; printf("%s\n",s); } return 0; }
相关文章推荐
- 1690 (Your)((Term)((Project))) 解题报告
- ZOJ 1423 POJ 1690 (Your)((Term)((Project)))
- poj 模拟 - 1690 (Your)((Term)((Project)))
- ZOJ 1423 (Your)((Term)((Project))) (POJ 1690) (模拟)
- POJ 1690 (Your)((Term)((Project))) 笔记
- ZOJ 1423 (Your)((Term)((Project))) (POJ 1690)
- poj 1690 (Your)((Term)((Project)))
- POJ 1690 - (Your)((Term)((Project)))
- POJ--1690 (Your)((Term)((Project)))(字符串处理)
- POJ 1690 (Your)((Term)((Project)))
- POJ1690 (Your)((Term)((Project)))
- POJ--1690 (Your)((Term)((Project)))(字符串处理)
- POJ 1690 (Your)((Term)((Project)))
- poj 1690 (Your)((Term)((Project)))
- POJ 1690 (Your)((Term)((Project)))
- ZOJ 1423 (Your)((Term)((Project))) (模拟+数据结构)
- (Your)((Term)((Project)))(字符串处理)
- 4000 poj1690 (Your)((Term)((Project))) (括号配对)
- (Your)((Term)((Project)))(字符串处理)
- ZOJ-1423-(Your)((Term)((Project))) 【模拟】