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. LINUX 硬盘命令

    1. 查看硬盘情况fdisk -l 每个Disk 为一个硬盘2. 挂在新硬盘fdisk /dev/sdb #硬盘地址Command (m for help):n #新建立分区Command actio ...

  2. 最小生成树练习1(克鲁斯卡尔算法Kruskal)

    今天刷一下水题练手入门,明天继续. poj1861 Network(最小生成树)新手入门题. 题意:输出连接方案中最长的单根网线长度(必须使这个值是所有方案中最小的),然后输出方案. 题解:本题没有直 ...

  3. uva 1220

    1220 - Party at Hali-Bula Time limit: 3.000 seconds Dear Contestant, I'm going to have a party at my ...

  4. 转载 网页打印时设置A4大小

    最近开发项目时遇到了网页打印的问题,这是问题之二,打印宽度设置 在公制长度单位与屏幕分辨率进行换算时,必须用到一个DPI(Dot Per Inch)指标. 经过我仔细的测试,发现了网页打印中,默认采用 ...

  5. CSS垂直居中对齐

    用CSS有多种方法实现垂直居中对齐.如果已知外部div的高度,不管是否知道内部div的高度,垂直居中实现起来很简单,但如果内部div高度是变量,如文字,垂直居中实现起来就比较复杂了,很可能需要使用ha ...

  6. 对于transform的新认识

    transform-origin是作用于该元素自己的,transform-origin(0px,0px),是以该元素自己本身的左上角(0px,0px)为圆心进行动作的.

  7. linux shell 单引号 双引号 反引号的区别

    一.单引号和双引号 首先, 单引号和双引号,都是为了解决中间有空格的问题. 因为空格在linux中时作为一个很典型的分隔符,比如 string1=this is a string,这样执行就会报错.为 ...

  8. spring mvc设置字符集过滤器

    <filter> <filter-name>springEncoding</filter-name> <filter-class> org.spring ...

  9. HookSSDT 通过HookOpenProcess函数阻止暴力枚举进程

    首先要知道Ring3层调用OpenProcess的流程 //当Ring3调用OpenProcess //1从自己的模块(.exe)的导入表中取值 //2Ntdll.dll模块的导出表中执行ZwOpen ...

  10. 在linux终端下打开pdf文件

    已经使用linux有很长的时间了,感觉linux的终端使用起来非常的方便.但是怎么在linux下用命令的方式打开pdf文件呢.我查了一下,打开pdf的命令是: evince [filename] 例如 ...