(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. CentOS6.4安装JDK,卸载自带的OpenJDK

    1.查看OpenJDK的安装包 $ rpm -qa |grep java java-1.6.0-openjdk-1.6.0.0-1.62.1.11.11.90.el6_4.x86_64 java-1. ...

  2. WAMP安装提示缺少 msvcr100.dll文件解决方法

    WAMP安装提示缺少wamp msvcr100.dll文件解决方法 因为wamp基于vs c++2010开发,需要提前安装这个组件才可以正常运行 微软官方组件下载地址: 32位:http://www. ...

  3. jsp跳转标签<jsp:forward>

    forward.jsp <%@ page language="java" contentType="text/html; charset=utf-8" p ...

  4. UVA 12405 Scarecrow (基础DP)

    题意: 给出一个1*N的矩阵(就是一行的格子),其中部分格子可以有草,部分无草,现在要求放置一些稻草人在某些格子上,每个稻草人可以覆盖3个连续格子,为使得有草的格子都能被覆盖,问最少放置几个稻草人. ...

  5. 【Python图像特征的音乐序列生成】思路的转变

    关于生成网络这边,可能会做一个深度的受限玻尔兹曼机,这样可以保证生成的音乐不会太相似. 情绪识别网络和生成网络的耦合,中间变量可能直接就是一个one-hot向量,用来标注指定的情绪,不做成坐标那种难以 ...

  6. [课堂总结]C++课堂总结(二)

    近期的面向对象程序设计的不容易记忆或者理解的东西进行一个总结,以后忘记了可以常来看下,C++是个很重要的东西,很多领域都用得到,加油,特种兵! 浅拷贝构造.深拷贝构造 浅拷贝构造是系统默认的拷贝构造函 ...

  7. samba修改smb.conf后,不需要重启服务,就可生效

    在修改完smb.conf后,不需要重启服务.在Centos7.3与Ubuntu18.04上验证都没有问题. 猜测可能的原因:samba是在客户端进行连接时,smb服务程序读取smb.conf配置文件信 ...

  8. Oracle Real Application Clusters (RAC)

    Oracle Real Application Clusters — 概述 包含 Oracle Real Application Clusters (RAC) 选件的 Oracle 数据库允许依托一组 ...

  9. Mysql查询指定用户并列排名 类似rank函数

    SELECT total.* FROM ( SELECT obj.uid, obj.score, CASE WHEN @rowtotal = obj.score THEN @rownum WHEN @ ...

  10. 2019.05.26 周日--《阿里巴巴 Java 开发手册》精华摘要

    一.写在开头 Java作为一个编程界最流行的语言之一,有着很强的生命力.代码的编写规范也是不容忽视的,今天,我就把自己阅读的国内的互联网巨头阿里巴巴的<阿里巴巴 Java 开发手册>一些精 ...