(Your)((Term)((Project)))

Time Limit: 1000MS Memory Limit: 10000K

Total Submissions: 3353 Accepted: 1256

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

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 <string.h>
#include <math.h>
#include <algorithm>
#include <stdlib.h>
#include <stack> using namespace std;
char a[300];
char b[300];
int tag[300];
int flag[300];
int len;
stack<int> s;
int find(int num)
{
int res=0;
for(int i=0;i<len;i++)
{
if(tag[i]==num)
res=i;
}
return res;
}
int fun(int x,int y)
{
for(int i=x;i<=y;i++)
if(a[i]=='+'||a[i]=='-')
return 0;
return 1;
}
int main()
{
int t;
scanf("%d",&t);
getchar();
while(t--)
{
memset(a,0,sizeof(a));
gets(b);
int tot=0;
int pot=0;
while(b[tot]!='\0')
{
if(b[tot]!=' ')
{
a[pot]=b[tot];
pot++;
}
tot++;
}
len=strlen(a);
while(!s.empty())
s.pop();
int cot=0;
memset(tag,0,sizeof(tag));
for(int i=0;i<len;i++)
{
if(a[i]=='(')
{
tag[i]=++cot;
s.push(i);
} else if(a[i]==')')
{
tag[i]=tag[s.top()];
s.pop();
}
else
tag[i]=0; }
memset(flag,0,sizeof(flag)); for(int i=0;i<len;i++)
{
if(a[i]=='('&&i==0)
flag[tag[i]]=1;
if(a[i]=='('&&a[i-1]=='+')
flag[tag[i]]=1;
// if(a[i]=='('&&a[i+2]==')')
//flag[tag[i]]=1;
if(a[i]=='('&&a[i-1]=='(')
flag[tag[i]]=1;
if(a[i]=='('&&(fun(i+1,find(tag[i])-1)==1)&&a[i-1]=='-')
flag[tag[i]]=1;
}
for(int i=0;i<len;i++)
if(flag[tag[i]]==0)
printf("%c",a[i]);
printf("\n");
}
return 0;
}

POJ--1690 (Your)((Term)((Project)))(字符串处理)的更多相关文章

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

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

  2. (Your)((Term)((Project)))

    Description You have typed the report of your term project in your personal computer. There are seve ...

  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. poj 1684 Lazy Math Instructor(字符串)

    题目链接:http://poj.org/problem?id=1686 思路分析:该问题为表达式求值问题,对于字母使用浮点数替换即可,因为输入中的数字只能是单个digit. 代码如下: #includ ...

  6. POJ 2406 Power Strings(字符串的最小循环节)

    题目链接:http://poj.org/problem?id=2406 题意:确定字符串最多是多少个相同的字串重复连接而成的 思路:关键是找到字符串的最小循环节 code: #include < ...

  7. POJ 1743 Musical Theme (字符串HASH+二分)

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15900   Accepted: 5494 De ...

  8. poj 3080 Blue Jeans【字符串处理+ 亮点是:字符串函数的使用】

    题目:http://poj.org/problem?id=3080 Sample Input 3 2 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCA ...

  9. POJ 1509 Glass Beads【字符串最小表示法】

    题目链接: http://poj.org/problem?id=1509 题意: 求循环字符串的最小表示. 分析: 浅析"最小表示法"思想在字符串循环同构问题中的应用 判断两字符串 ...

随机推荐

  1. Oauth2.0(二):开放平台

    上一节说到Oauth2.0 的交互模型.模型涉及到三方:资源拥有者.客户端.服务提供方.其中,服务提供方包含两个角色:鉴权服务器和资源服务器.鉴权服务器负责对用户进行认证,并授权给客户端权限.认证这一 ...

  2. js 或 且 非

    给定 x=6 以及 y=3,下表解释了逻辑运算符: 运算符 描述 例子 && and (x < 10 && y > 1) 为 true || or (x== ...

  3. 【DVWA】Web漏洞实战之File Upload

    File Upload File Upload,即文件上传漏洞,一般的上传漏洞可能是未验证上传后缀 或者是验证上传后缀被bypass 或者是上传的文件验证了上传后缀但是文件名不重命名. LOW 直接上 ...

  4. 使用tinyproxy搭建http代理

    一.前言   二.搭建环境 * Linux laptop 2.6.32-45-generic #100-Ubuntu SMP Wed Nov 14 10:41:11 UTC 2012 i686 GNU ...

  5. Jsoup(一)-- HelloWorld

    1.简介 jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQuery的操作方法来取出和操作数据. ...

  6. Codeforces Round #363 (Div. 1) B. Fix a Tree 树的拆环

    题目链接:http://codeforces.com/problemset/problem/698/B题意:告诉你n个节点当前的父节点,修改最少的点的父节点使之变成一棵有根树.思路:拆环.题解:htt ...

  7. RabbitMQ与java、Spring结合实例详细讲解

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文介绍了rabbitMq,提供了如何在Ubuntu下安装RabbitMQ 服务的方法. ...

  8. php文件的处理和操作

    好长时间没有看php手册了,有些关于文件操作方面的知识点发现从没有学过,现补习一下,顺便整理一下: 1.文件的打开:fopen()   此函数的第一个参数含有要打开的文件的名称,第二个参数规定了使用哪 ...

  9. 【C#新特性】不用out ref同时返回多个值-元组Tuple

    元组Tuple,它是一种固定成员的泛型集合 下面先看看官方的一个使用例子: 创建一个包含7个元素的Tuple数组 // Create a 7-tuple. , , , , , );// Display ...

  10. 关于SQL优化(转载,格式有调整)

    一.问题的提出 在应用系统开发初期,由于开发数据库数据比较少,对于查询SQL语句,复杂视图的的编写等体会不出SQL语句各种写法的性能优劣,但是如果将应用 系统提交实际应用后,随着数据库中数据的增加,系 ...