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

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=260;
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,0,sizeof(vis));
        memset(kk,-1,sizeof(kk));
        int i,j;
        for(i=0,j=0;a[i];i++)
            if(a[i]!=‘ ‘) str[j++]=a[i];
        str[j]=0;
        for(i=0;str[i];i++)
        {
            if(str[i]==‘)‘&kk[i]==-1)
            {
                for(j=i-1;j>=0;j--)
                {
                    if(str[j]==‘(‘&&vis[j]==0)
                    {
                        kk[i]=j;
                        vis[j]=1;break;
                    }
                }
               // vis[i]=1;
            }

        }
        int flag,del[N];
        memset(del,0,sizeof(del));

        for(i=0;str[i];i++)
        {
            if(!del[i]&&str[i]==‘)‘)
            {
                flag=0;
                for(j=i-1;j>kk[i];j--)

                    if(str[j]==‘+‘||str[j]==‘-‘)
                    {
                        flag=1;break;
                    }
                    if(kk[i]==0||str[kk[i]-1]==‘-‘&&flag==0||str[kk[i]-1]!=‘-‘)
                        del[kk[i]]=1,del[i]=1;

            }
        }
        for( i=0;str[i];i++)
        {
            if(del[i]) continue;
            printf("%c",str[i]);
        }
        printf("\n");

    }
    return 0;
}
时间: 2024-10-14 15:32:14

(Your)((Term)((Project)))的相关文章

UVALive 6511 Term Project

Term Project Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ID: 651164-bit integer IO format: %lld      Java class name: Main 解题:强连通分量 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 1

Tarjan UVALive 6511 Term Project

题目传送门 1 /* 2 题意:第i个人选择第a[i]个人,问组成强联通分量(自己连自己也算)外还有多少零散的人 3 有向图强联通分量-Tarjan算法:在模板上加一个num数组,记录每个连通分量的点数,若超过1,则将连通点数相加 4 用总点数-ans则是零散的点 5 详细解释:http://www.bubuko.com/infodetail-927304.html 6 */ 7 #include <cstdio> 8 #include <cstring> 9 #include &

Dynare/Matlab Project International Finance

Dynare/Matlab ProjectInternational Finance (Open Economy Macroeconomics)NOTE: “Handing in solutions to this project” involves scanning and emailing your analyticalsolutions (to be written in directly in this handout), emailing me the Dynare code(.mod

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

分布式系统学习

两个帖子:知乎, Quora @严林 推荐的三篇论文 1.Sinfonia: A New Paradigm for Building Scalable Distributed Systems,这篇论文是SOSP2007的Best Paper,阐述了一种构建分布式文件系统的范式方法,个人感觉非常有用.淘宝在构建TFS.OceanBase和Tair这些系统时都充分参考了这篇论文. 2. The Chubby lock service for loosely-coupled distributed s

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

此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276,1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740(博弈),1742, 1887, 1926(马尔科夫矩阵,求平衡), 1936, 1952, 1953, 1958, 1959, 1962, 1975,

Soj题目分类

-----------------------------最优化问题------------------------------------- ----------------------常规动态规划  SOJ1162 I-Keyboard  SOJ1685 Chopsticks SOJ1679 Gangsters SOJ2096 Maximum Submatrix  SOJ2111 littleken bg SOJ2142 Cow Exhibition  SOJ2505 The County

zoj题目分类

饮水思源---zoj 转载自:http://bbs.sjtu.edu.cn/bbscon,board,ACMICPC,file,M.1084159773.A.html 注:所有不是太难的题都被归成了“简单题”,等到发现的时候已经太晚了,我太死脑筋 了……:( 有些题的程序我找不到了,555……:( SRbGa的题虽然都很经典……但是由于其中的大部分都是我看了oibh上的解题报告后做 的,所以就不写了…… 题目排列顺序没有规律……:( 按照个人感觉,最短路有的算做了DP,有的算做了图论. 有些比较

Regionals 2013 Asia - Daejeon (部分题目题解)

题目链接:Regionals 2013 Asia - Daejeon 6500 Boxes 题意:将箱子(矩阵的1)全移动到矩阵的底部需要几步 思路:按列从下到上统计.(n,m)的矩阵,移动一个箱子(x,y),如果有c个箱子在底部,那么移动该箱子的步数是(n-x-c-1). AC代码: #include <stdio.h> #include <string.h> int mp[110][110]; int main() { int t; int i,j,n,m; scanf(&qu