【暴力】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型的数 ...
随机推荐
- Elasticsearch报错
[2018-07-12T10:32:47,642][INFO ][o.e.b.BootstrapChecks ] [VfCcJIq] bound or publishing to a non-loop ...
- 7月24号day16总结
一开始显示出现问题,js路径不能应用,因为用的是springMVC框架书写,所以有路径的保护和静态引用地址时需要注意的地方 今天进行了最后项目的优化,包括map清洗数据部分的代码和echarts显示的 ...
- source改变当前路径
转摘自:http://hi.baidu.com/homappy/item/90e416525d2faf958c12edb7 Shell 脚本执行有三种方法 bash 脚本名 sh 脚本名 chmod ...
- 51Nod 1421
1421 最大MOD值 有一个a数组,里面有n个整数.现在要从中找到两个数字(可以是同一个) ai,aj ,使得 ai mod aj 最大并且 ai ≥ aj. Input 单组测试数据. 第一行包含 ...
- 数据结构&图论:K短路-可持久化可并堆
本来A*就可以搞定的题,为了怕以后卡复杂度,找了个这么个方法 现阶段水平不够就不补充算法分析部分了 对于图G,建立一个以终点t为起点的最短路径构成的最短路径树 (就是反着跑一遍最短路,然后对于一个不为 ...
- 谈谈openstack部署规模问题
理论上,单个openstack已设计成可水平扩展的系统,只要数据库足够快,消息总线足够多资源等,一个openstack系统可管理上千台物理服务器是没有问题的. 但是单个openstack规模大了之后, ...
- windows启动redis服务
参考:https://www.cnblogs.com/M-LittleBird/p/5902850.html 使用python的pip install redis以后还需要下载安装redis安装文件才 ...
- CentOS 7 部署nginx
**二进制安装 安装Nginx源 rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el ...
- [Leetcode Week9]Word Break
Word Break 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-break/description/ Description Given ...
- ubuntu下wireshark+scapy+tcpreply
安装wireshark命令: sudo apt-get install wireshark 运行打开wireshark命令: sudo wireshark(一定要以超级权限打开) 正确打开的窗口应该默 ...