【暴力】UVALive - 4882 - Parenthesis
就不断地扫整个序列,如果发现多余的括号就删除。大概复杂度还是O(n²)左右。如何判断不合法请详见代码。
To a computer, there is no difference between the expression (((x)+(y))(t))and (x+y)t; but, to a human, the latter is easier to read. When writing automatically generated expressions that a human may have to read, it is useful to minimize the number of parentheses in an expression. We assume expressions consist of only two operations: addition (+) and multiplication (juxtaposition), and these operations act on single lower-case letter variables only. Specifically, here is the grammar for an expression E:
E : P | P '+' E
P : F | F P
F : V | '(' E ')'
V : 'a' | 'b' | .. | 'z'
The addition (+, as in x+y) and multiplication (juxtaposition, as in xy) operators are associative: x+(y+z)=(x+y)+z=x+y+z and x(yz)=(xy)z=xyz. Commutativity and distributivity of these operations should not be assumed. Parentheses have the highest precedence, followed by multiplication and then addition.
Input
The input consists of a number of cases. Each case is given by one line that satisfies the grammar above. Each expression is at most 1000 characters long.
Output
For each case, print on one line the same expression with all unnecessary parentheses removed.
Sample input
x
(x+(y+z))
(x+(yz))
(x+y(x+t))
x+y+xt
Sample Output
x
x+y+z
x+yz
x+y(x+t)
x+y+xt
#include<cstdio>
#include<iostream>
#include<string>
#include<stack>
using namespace std;
stack<int>st;
string s;
int n;
bool check(int l,int r)
{
int cnt=0;
for(int i=l;i<=r;++i)
if(s[i]=='(')
++cnt;
else if(s[i]==')')
--cnt;
else if(s[i]=='+' && cnt==0)
return 0;
return 1;
}
int main()
{
while(cin>>s)
{
while(1)
{
bool flag=0;
n=s.length();
while(!st.empty())
st.pop();
for(int j=0;j<n;++j)
if(s[j]=='(')
st.push(j);
else if(s[j]==')')
{
int x=st.top(); st.pop();
if((x==0 || s[x-1]=='+' || s[x-1]=='(')
&& (j==n-1 || s[j+1]=='+' || s[j+1]==')'))
{
s.erase(x,1);
s.erase(j-1,1);
flag=1;
break;
}
else if(check(x+1,j-1))
{
s.erase(x,1);
s.erase(j-1,1);
flag=1;
break;
}
}
if(!flag)
break;
}
cout<<s<<endl;
}
return 0;
}
【暴力】UVALive - 4882 - Parenthesis的更多相关文章
- Parenthesis UVALive - 4882 删除不必要的括号序列,模拟题 + 数据
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- Gym 100299C && UVaLive 6582 Magical GCD (暴力+数论)
题意:给出一个长度在 100 000 以内的正整数序列,大小不超过 10^ 12.求一个连续子序列,使得在所有的连续子序列中, 它们的GCD值乘以它们的长度最大. 析:暴力枚举右端点,然后在枚举左端点 ...
- UVALive 7077 - Little Zu Chongzhi's Triangles(暴力)
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- UVaLive 6625 Diagrams & Tableaux (状压DP 或者 DFS暴力)
题意:给一个的格子图,有 n 行单元格,每行有a[i]个格子,要求往格子中填1~m的数字,要求每个数字大于等于左边的数字,大于上边的数字,问有多少种填充方法. 析:感觉像个DP,但是不会啊...就想暴 ...
- UVaLive 6623 Battle for Silver (最大值,暴力)
题意:给定一个图,让你找一个最大的子图,在这个子图中任何两点都有边相连,并且边不交叉,求这样子图中权值最大的是多少. 析:首先要知道的是,要想不交叉,那么最大的子图就是四个点,否则一定交叉,然后就暴力 ...
- UVaLive 6855 Banks (水题,暴力)
题意:给定 n 个数,让你求最少经过几次操作,把所有的数变成非负数,操作只有一种,变一个负数变成相反数,但是要把左右两边的数加上这个数. 析:由于看他们AC了,时间这么短,就暴力了一下,就AC了... ...
- UVALive 7070 The E-pang Palace 暴力
The E-pang Palace Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/problem ...
- UVALive 7070 The E-pang Palace(暴力)
实话说这个题就是个暴力,但是有坑,第一次我以为相含是不行的,结果WA,我加上相含以后还WA,我居然把这两个矩形的面积加在一块了吗,应该取大的那一个啊-- 方法就是枚举对角线,为了让自己不蒙圈,我写了一 ...
- UVALive - 4026 Difficult Melody(暴力)
我这个英语学渣又把题给翻译错了……(话说,六级差十分没有过,好心疼T T),题目中说的P和Q都是计算game的个数,我以为是出现的次数,各种wa..后来调整了以后又是各种wa,原来是double型的数 ...
随机推荐
- Codeforces Round #348 (VK Cup 2016 Round 2, Div. 2 Edition) A
A. Little Artem and Presents time limit per test 2 seconds memory limit per test 256 megabytes input ...
- JSOI2008 星球大战 [并查集]
题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系. 某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过特殊的以太隧 ...
- [lucene系列笔记2]在eclipse里初步使用lucene的索引和查询功能
首先,new一个java project,名字叫做LuceneTools. 然后,在project里new一个class,名字叫做IndexFiles.这个类用来给文件建索引(建好索引以后就可以高效检 ...
- 7月17号day9总结
今天学习过程和小结 今天学习了如何使用idea操作hdfs. public class HDFSTest { Configuration configuration; FileSystem ...
- mysql_存储过程和函数
存储过程和函数 1.什么是存储过程和函数 存储过程和函数是事先经过编译并存储在数据库中的一段SQL语句集合,调用存储过程和函数可以简化应用开发人员的很多工作,减少数据在数据库和应用服务器之间的传输,对 ...
- white-space——处理元素内的空白
定义和用法 white-space 属性设置如何处理元素内的空白.这个属性声明建立布局过程中如何处理元素中的空白符.值 pre-wrap 和 pre-line 是 CSS 2.1 中新增的. 默认 ...
- Nginx 默认配置解析
# For more information on configuration, see: # * Official English Documentation: http://nginx.org/e ...
- 【洛谷 P4168】[Violet]蒲公英(分块)
题目链接 题目大意:给定\(n\)个数和\(m\)个求区间众数的询问,强制在线 这题我\(debug\)了整整一个下午啊..-_- 从14:30~16:45终于\(debug\)出来了,\(debug ...
- [bzoj1251]序列终结者——splay
题目大意 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技 ...
- Python小程序之购物车
需求: 用户入口: 1.商品信息放在文件中,从文件中读取 2.已购商品,余额记录,第一要输入起始金额,以后不需要二次输入 商家入口: 2.可以添加商品,修改商品价格 # Author:Lee Siri ...