博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 1690 (Your)((Term)((Project)))
阅读量:4682 次
发布时间:2019-06-09

本文共 3903 字,大约阅读时间需要 13 分钟。

(Your)((Term)((Project)))
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 2912   Accepted: 1084

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: 
  1. The input file contains a number of expressions, each in one separate line. 
  2. Variables in the expressions are only single uppercase letters. 
  3. 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) 题目大意:去除一个表达式中的括号,使其含义不变。 解题方法:通过分析可知,可去除的括号有三类,1.最外层括号 2.前面为加号的括号 3.括号中只有一个字母的括号。
#include 
#include
#include
using namespace std;int main(){ char str[300]; char str1[300]; int pre[300]; int visited[300]; int del[300]; int nCase; scanf("%d", &nCase); getchar(); while(nCase--) { gets(str); memset(pre, 0, sizeof(pre)); memset(visited, 0, sizeof(visited)); memset(del, 0, sizeof(del)); int nLen = strlen(str); int nCount = 0; for (int i = 0; i < nLen; i++) { if (str[i] != ' ') { str1[nCount++] = str[i]; } } str1[nCount] = '\0'; nLen = strlen(str1); for (int i = 0; i < nLen; i++) { if (str1[i] == ')') { for (int j = i - 1; j >= 0; j--) { if (str1[j] == '(' && !visited[j]) { pre[i] = j; visited[j] = 1; break; } } } } for (int i = 0; i < nLen; i++) { if (str1[i] == ')') { int flag = 0; for (int j = i - 1; j > pre[i]; j--) { if (str1[j] == '+' || str1[j] == '-') { flag = 1; break; } } if (str1[pre[i]] == '(' && (str1[pre[i] - 1] != '-' || pre[i] == 0 || !flag)) { del[i] = del[pre[i]] = 1; } } } for (int i = 0; i < nLen; i++) { if (!del[i]) { printf("%c", str1[i]); } } printf("\n"); } return 0;}

 

转载于:https://www.cnblogs.com/lzmfywz/p/3212569.html

你可能感兴趣的文章
【JDK源码分析】 String.join()方法解析
查看>>
【SICP练习】112 练习3.28
查看>>
python--注释
查看>>
前端资源链接 ...
查看>>
yum install ntp 报错:Error: Package: ntp-4.2.6p5-25.el7.centos.2.x86_64 (base)
查看>>
leetcode-Single Number-136
查看>>
CF715C Digit Tree
查看>>
二分法练习1
查看>>
QT 制作串口调试小助手----(小白篇)
查看>>
前端MVC实践之hellorocket——by张舒彤
查看>>
OptimalSolution(2)--二叉树问题(3)Path路径问题
查看>>
IPC 之 Messenger 的使用
查看>>
macos 下usb键盘问题.
查看>>
SQL函数学习(十六):STUFF()函数
查看>>
Apache Hadoop 和Hadoop生态圈
查看>>
Ctrl+Enter 选中文本提交
查看>>
android WIFI
查看>>
常用的匹配正则表达式和实例
查看>>
小组成员及其git链接
查看>>
SQL case when else
查看>>