POJ--1690 (Your)((Term)((Project)))(字符串处理)
(Your)((Term)((Project))) Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3353 Accepted: 1256 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: The input file contains a number of expressions, each in one separate line. Variables in the expressions are only single uppercase letters. 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 <string.h>
#include <math.h>
#include <algorithm>
#include <stdlib.h>
#include <stack>
using namespace std;
char a[300];
char b[300];
int tag[300];
int flag[300];
int len;
stack<int> s;
int find(int num)
{
int res=0;
for(int i=0;i<len;i++)
{
if(tag[i]==num)
res=i;
}
return res;
}
int fun(int x,int y)
{
for(int i=x;i<=y;i++)
if(a[i]=='+'||a[i]=='-')
return 0;
return 1;
}
int main()
{
int t;
scanf("%d",&t);
getchar();
while(t--)
{
memset(a,0,sizeof(a));
gets(b);
int tot=0;
int pot=0;
while(b[tot]!=' ')
{
if(b[tot]!=' ')
{
a[pot]=b[tot];
pot++;
}
tot++;
}
len=strlen(a);
while(!s.empty())
s.pop();
int cot=0;
memset(tag,0,sizeof(tag));
for(int i=0;i<len;i++)
{
if(a[i]=='(')
{
tag[i]=++cot;
s.push(i);
}
else if(a[i]==')')
{
tag[i]=tag[s.top()];
s.pop();
}
else
tag[i]=0;
}
memset(flag,0,sizeof(flag));
for(int i=0;i<len;i++)
{
if(a[i]=='('&&i==0)
flag[tag[i]]=1;
if(a[i]=='('&&a[i-1]=='+')
flag[tag[i]]=1;
// if(a[i]=='('&&a[i+2]==')')
//flag[tag[i]]=1;
if(a[i]=='('&&a[i-1]=='(')
flag[tag[i]]=1;
if(a[i]=='('&&(fun(i+1,find(tag[i])-1)==1)&&a[i-1]=='-')
flag[tag[i]]=1;
}
for(int i=0;i<len;i++)
if(flag[tag[i]]==0)
printf("%c",a[i]);
printf("n");
}
return 0;
}
- Apache虚拟主机-解惑篇
- Hadoop生态圈和各组件的启动、关闭脚本介绍
- CUDA入门
- 关于cuda拷贝的速度测试
- VS报错:DEBUG Assertion Failed!
- cuda&vs2010的属性配置
- CUDA中自动初始化显卡设备宏
- 如何在cuda内核函数中产生随机数(host端调用,device端产生)
- [翻译]CURAND Libaray--Host API--(2)
- [翻译]CURAND Libaray--Host API--(1)
- C/C++ 遇到0xcccccccc访问冲突
- Win7下Eclipse开发hadoop应用程序环境搭建
- Oracle修改监听IP地址
- Oracle卸载
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Mac下快速搭建PHP开发环境步骤详解
- PHP常用工具函数小结【移除XSS攻击、UTF8与GBK编码转换等】
- Ajax+PHP实现的模拟进度条功能示例
- python实例化对象的具体方法
- PHP5.6.8连接SQL Server 2008 R2数据库常用技巧分析总结
- YII框架关联查询操作示例
- Keras之fit_generator与train_on_batch用法
- django美化后台django-suit的安装配置操作
- python读取excel进行遍历/xlrd模块操作
- 浅谈Keras的Sequential与PyTorch的Sequential的区别
- python 制作python包,封装成可用模块教程
- 使用python脚本自动生成K8S-YAML的方法示例
- Python虚拟环境库virtualenvwrapper安装及使用
- tp5框架内使用tp3.2分页的方法分析
- YII框架常用技巧总结