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-B
A-(B+C)

【题意】把给出表达式改为规范的表达式

【思路】三种情况不用加括号

1.整个表达式不用用括号括起来

2.括号内没有运算不用括起来

3。括号前是加号不用括起来

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=;
int vis[N],kk[N];
char a[N],str[N];
int main()
{
int t;
scanf("%d",&t);
getchar();//吸收回车键
while(t--)
{
gets(a);//读入整行,以\n或EOF为结束
//scanf("%s",a);
memset(vis,,sizeof(vis));
memset(kk,-,sizeof(kk));
int i,j;
for(i=,j=;a[i];i++)
if(a[i]!=' ') str[j++]=a[i];
str[j]=;
for(i=;str[i];i++)
{
if(str[i]==')'&kk[i]==-)
{
for(j=i-;j>=;j--)
{
if(str[j]=='('&&vis[j]==)
{
kk[i]=j;
vis[j]=;break;
}
}
// vis[i]=1;
} }
int flag,del[N];
memset(del,,sizeof(del)); for(i=;str[i];i++)
{
if(!del[i]&&str[i]==')')
{
flag=;
for(j=i-;j>kk[i];j--) if(str[j]=='+'||str[j]=='-')
{
flag=;break;
}
if(kk[i]==||str[kk[i]-]=='-'&&flag==||str[kk[i]-]!='-')
del[kk[i]]=,del[i]=; }
}
for( i=;str[i];i++)
{
if(del[i]) continue;
printf("%c",str[i]);
}
printf("\n"); }
return ;
}

(Your)((Term)((Project)))的更多相关文章

  1. POJ--1690 (Your)((Term)((Project)))(字符串处理)

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

  2. POJ 1690 (Your)((Term)((Project)))

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

  3. Tarjan UVALive 6511 Term Project

    题目传送门 /* 题意:第i个人选择第a[i]个人,问组成强联通分量(自己连自己也算)外还有多少零散的人 有向图强联通分量-Tarjan算法:在模板上加一个num数组,记录每个连通分量的点数,若超过1 ...

  4. UVALive 6511 Term Project

    Term Project Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Origi ...

  5. ZOJ 1423 (Your)((Term)((Project))) (模拟+数据结构)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=423 Sample Input 3(A-B + C) - (A+(B ...

  6. Storm(3) - Calculating Term Importance with Trident

    Creating a URL stream using a Twitter filter Start by creating the project directory and standard Ma ...

  7. Distributed Databases and Data Mining: Class timetable

    Course textbooks Text 1: M. T. Oszu and P. Valduriez, Principles of Distributed Database Systems, 2n ...

  8. 别人整理的DP大全(转)

    动态规划 动态规划 容易: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ...

  9. dp题目列表

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

随机推荐

  1. 190. Reverse Bits -- 按位反转

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  2. 51nod 1043 幸运号码(数位dp)

    题目链接:51nod 1043 幸运号码 题解:dp[i][j]表示 i 个数和为 j 的总数(包含0开头情况) dp[i][j] = dp[i-1][j-k] i & 1 :这里用滚动数组节 ...

  3. java 分析方法调用过程

    StackTraceElement[] s = new Exception().getStackTrace(); for(int i=0;i<s.length;i++) System.out.p ...

  4. Snappy压缩

    Snappy压缩时,碰到不能解压问题,所用服务器Tomcat8.经验证,降低Tomcat版本为7,才可正常解压文件. 若碰到偶尔不能解压的问题,试着换个浏览器试试.

  5. Computer Science Courses – Yan Yan

    CS: Compilers / Programming Languages Course Title Fundamentals of C++ Language Programming Textbook ...

  6. qml操作播放器

    现在增加了一个filter属性,所以可以很好和opencv结合.转一篇文章(http://blog.qt.io/blog/2015/03/20/introducing-video-filters-in ...

  7. 关于职位的解释---转CSDN的文章

    摘要我在IT职场打滚超过15年了,从小小的程序员做到常务副总.相对于其它行业,IT职场应该算比较光明的了,但也陷阱重重,本文说说我的亲身体会,希望大家能在IT职场上战无不胜! 通用法则 法则1:忍耐是 ...

  8. C#中三种定时器对象的比较

    ·关于C#中timer类 在C#里关于定时器类就有3个1.定义在System.Windows.Forms里2.定义在System.Threading.Timer类里3.定义在System.Timers ...

  9. STM32-AFIO

    只有使用了AFIO的事件控制寄存器.AFIO的重映射功能以及外部中断(EXTI)控制寄存器才需要开启AFIO的时钟,STM32参考手册从来没说过使用IO的复用功能就一定要开启AFIO时钟,这是个误区.

  10. CSS盒子模型和IE浏览器

    CSS盒模型图解 下面是一幅关于应用了CSS的元素是如何显示它的尺寸的图示. 在本篇文章中,所有的浏览器在计算盒模型总宽度时处理margin属性的方式都是一致的,所以我们将更多的精力放在padding ...