zoukankan      html  css  js  c++  java
  • (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);//读入整行,以
    或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("
    ");
    
        }
        return 0;
    }
  • 相关阅读:
    Querying for Event Information
    通过注册表查询 .Net Framework 的版本
    [Batch脚本] if else 的格式
    逆天技能
    财运是靠自己争取的,而财商是可以通过后天学习提高的
    必须冒着可能付出惨痛代价的风险前进,否则你就只能永远做个井底之蛙
    财商低的六种表现
    中国大唐集团公司 主要经营范围
    中国大唐集团公司是2002年12月29日在原国家电力公司部分企事业单位基础上组建而成的特大型发电企业集团
    中国大唐集团公司在役及在建资产分布在全国31个省区市以及境外
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/5868430.html
Copyright © 2011-2022 走看看