(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-B
A-(B+C)
题目大意:去除一个表达式中的括号,使其含义不变。
解题方法:通过分析可知,可去除的括号有三类,1.最外层括号 2.前面为加号的括号 3.括号中只有一个字母的括号。
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std; int main()
{
char str[];
char str1[];
int pre[];
int visited[];
int del[];
int nCase;
scanf("%d", &nCase);
getchar();
while(nCase--)
{
gets(str);
memset(pre, , sizeof(pre));
memset(visited, , sizeof(visited));
memset(del, , sizeof(del));
int nLen = strlen(str);
int nCount = ;
for (int i = ; i < nLen; i++)
{
if (str[i] != ' ')
{
str1[nCount++] = str[i];
}
}
str1[nCount] = '\0';
nLen = strlen(str1);
for (int i = ; i < nLen; i++)
{
if (str1[i] == ')')
{
for (int j = i - ; j >= ; j--)
{
if (str1[j] == '(' && !visited[j])
{
pre[i] = j;
visited[j] = ;
break;
}
}
}
}
for (int i = ; i < nLen; i++)
{
if (str1[i] == ')')
{
int flag = ;
for (int j = i - ; j > pre[i]; j--)
{
if (str1[j] == '+' || str1[j] == '-')
{
flag = ;
break;
}
}
if (str1[pre[i]] == '(' && (str1[pre[i] - ] != '-' || pre[i] == || !flag))
{
del[i] = del[pre[i]] = ;
}
}
}
for (int i = ; i < nLen; i++)
{
if (!del[i])
{
printf("%c", str1[i]);
}
}
printf("\n");
}
return ;
}

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

  1. (Your)((Term)((Project)))

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

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

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

  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. 专题:DP杂题1

    A POJ 1018 Communication System B POJ 1050 To the Max C POJ 1083 Moving Tables D POJ 1125 Stockbroke ...

  7. poj 动态规划题目列表及总结

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

  8. poj动态规划列表

    [1]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 13 ...

  9. POJ 动态规划题目列表

    ]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 1322 ...

随机推荐

  1. Codeforces 724 G Xor-matic Number of the Graph 线性基+DFS

    G. Xor-matic Number of the Graph http://codeforces.com/problemset/problem/724/G 题意:给你一张无向图.定义一个无序三元组 ...

  2. 程序员的智囊库系列之2----网站框架(framework)

    程序员的智囊库系列之2--网站框架(framework) 这是程序员的智囊库系列的第二篇文章.上一篇文章讲了服务器与运维相关的工具,这篇文章我们将介绍几个搭建网站的框架: django express ...

  3. APT和它的超级牛力

    当你在使用apt时,例如“apt -h”会提示“本APT具有超级牛” 先把牛放一放,先学习以下关于APT的知识. APT 高级打包工具(英语:Advanced Packaging Tools,缩写为A ...

  4. 2018.3.31 java中的递归

    java中的递归 1.概念 定义一个方法时,出现本方法调用本方法的过程,称之为递归 2.特点 必然有一个边界条件 使用递归代码往往更简洁,可读性强 3.什么时候使用递归 n的阶乘和n的累加定义 f(n ...

  5. tmpfs与内存盘

    如何快速的吃掉一段内存:通过tmpfs来划一片领地,再用dd去粗暴占用mount -t tmpfs -o size=512M tmpfs /mnt/demodd if=/dev/zero of=/mn ...

  6. PAT (Basic Level) Practise (中文)- 1008. 数组元素循环右移问题 (20)

    一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0A1……AN-1)变换为(AN-M …… AN-1 A0  ...

  7. sql server 定时备份 脚本

    ) DECLARE @date DATETIME SELECT @date = GETDATE() SELECT @filename = 'G:\backup\NewPlulishSQL-' + CA ...

  8. iOS8之后,UITableViewRowAction实现滑动多个按钮

    #pragma mark - View lifeCycle - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = ...

  9. 概括的描述一下Spring注册流程

    Spring经过大神们的构思.编码,日积月累而来,所以,对其代码的理解也不是一朝一夕就能快速完成的.源码学习是枯燥的,需要坚持!坚持!坚持!当然也需要技巧,第一遍学习的时候,不用关注全部细节,不重要的 ...

  10. 【windows】win7 sp1 系统语言中英文切换

    注:Windows 7 Ultimate and Windows 7 Enterprise (旗舰版和企业版) 可以直接在控制面板/地区和语言中修改显示语言,其他系统不行 进入网站下载相关的MUI包安 ...