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. javaWeb学习之运用myeclipse结合tomcat开发一些简单的jsp和service

    servlet是什么?     servlet是java服务器端编程.不同于我们之前写的一般的java应用程序,Servlet程序是运行在服务器上的,服务器有很多种.....现在只是用过 tomcat ...

  2. C++数据类型范围

    C++中有很多基本的数据类型,我们在使用过程中需要根据所需要存储数据的范围的不同而选择恰当的数据类型. Visual C++ 32 位和 64 位编译器可识别本文后面的表中的类型. int (unsi ...

  3. Java 集合系列 04 LinkedList详细介绍(源码解析)和使用示例

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  4. 创建PO

    FORM FRM_CREATE_PO USING P_POSNR CHANGING P_EBELN. DATA: LV_VENDOR TYPE LIFNR, LV_ITEM TYPE EBELP, L ...

  5. Vim经典讲解

    http://blog.csdn.net/niushuai666/article/details/7275406

  6. poi 读取 excel(.xlsx) 2007及以上版本

    1.注意的一点是sh.getLastRowNum(),比实际的行数少一行 涉及到的包:

  7. 指定socket文件连接mysql

    1.利用ps aux |grep mysql  查看--socket 路径 2.创建软连接.创建文件 3.登录成功

  8. js模拟快捷键操作表单

    <html> <head> </head> <body> <script> //键盘快捷键提交表单ctrl+s document.onkey ...

  9. 二模 (1) day1

    第一题: 题目大意:给出N(N<=50)个小于1000的正整数Ai,和一个正整数max,和一个整数cur,从前往后依次对每个Ai,可以让cur+Ai 或者 cur-Ai,但是结果不能大于max, ...

  10. hdu 1030 Delta-wave (C++, 0ms, explanatory comments.) 分类: hdoj 2015-06-15 12:21 45人阅读 评论(0) 收藏

    problem description http://acm.hdu.edu.cn/showproblem.php?pid=1030 #include <cstdio> #include ...