(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. android 内存泄露测试

    Android 程序由java语言编写,android的内存管理与java相似,通过new为对象分配内存,所有对象在java堆内分配空间,对象回收有个垃圾回收器来完成.GC就是垃圾收集的意思(Gaba ...

  2. 2018.2.28 PHP中使用jQuery+Ajax实现分页查询多功能如何操作

    PHP中使用jQuery+Ajax实现分页查询多功能如何操作 1.首先做主页Ajax_pag.php 代码如下 <!DOCTYPE html> <html> <head& ...

  3. GCD之dispatch queue

    GCD之dispatch queue iOS中多线程编程工具主要有: NSThread NSOperation GCD 这三种方法都简单易用,各有千秋.但无疑GCD是最有诱惑力的,因为其本身是appl ...

  4. Angular - Can't bind to 'ngModel' since it isn't a known property of 'input'.

    用[(ngModel)]="xxx"双向绑定,如:控制台报错:Can't bind to 'ngModel' since it isn't a known property of ...

  5. mysql 添加数据如果数据存在就更新ON DUPLICATE KEY UPDATE和REPLACE INTO

    #下面建立game表,设置name值为唯一索引. CREATE TABLE `game` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar( ...

  6. python3和Python2的区别

    一.print函数 python2 的 print 声明已经被 print() 函数取代了,这意味着我们必须包装我们想打印在小括号中的对象 二.通过input()解析用户的输入 python3中inp ...

  7. jquery.imgpreload.min.js插件实现页面图片预加载

    页面分享地址: http://wenku.baidu.com/link?url=_-G8miwbgDmEj6miyFtjit1duJggBCJmFjR2jky_G1VftD9eS9kwGOlFWAOR ...

  8. java代码导出数据到Excel、js导出数据到Excel(三)

     jsp内容忽略,仅写个出发按钮:          <button style="width: 100px" onclick="expertExcel()&quo ...

  9. LeetCode(292) Nim Game

    题目 You are playing the following Nim Game with your friend: There is a heap of stones on the table, ...

  10. LeetCode(120) Triangle

    题目 Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacen ...