【暴力】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型的数 ...
随机推荐
- UVA 11995 STL 使用
There is a bag-like data structure, supporting two operations: 1 x Throw an element x into the bag. ...
- 怎么利用idea自带的工具,不需要 重启tomcat或则其他服务,js代码自动生效
idea中有一个工具:可以直接upload,能让你修改的界面直接可以看到,不需要重启服务. 依次点击的按钮如下: 点击进入的界面这个填的只是一个示例,在各位的电脑上肯定不行,大家依据实际情况填写.
- lwIP内存管理机制
lwip的内存管理机制,我们以enet_lwip这个例程为例. 在使用lwip的时候,我们可以使用两种形式的内存,一种是heap(mem.c文件-mem_malloc()),一种是pool(memp. ...
- 定时导出用户数据(expdp,impdp)
一 定时导出数据: #!/bin/bash############################################################################### ...
- python并发进程
1 引言 2 创建进程 2.1 通过定义函数的方式创建进程 2.2 通过定义类的方式创建进程 3 Process中常用属性和方法 3.1 守护进程:daemon 3.2 进程终结于存活检查:termi ...
- CF502C The Phone Number
C. The Phone Number time limit per test 1 second memory limit per test 256 megabytes Mrs. Smith ...
- 愚蠢的LCAAAAA~~~~(>_<)~~~~
很愤怒!特别愤怒!超级愤怒!!! 我LCA居然错了!!而且是那种特别愚蠢的错误 我把代码都交错了!!! silasila 话不多说,代码上特别详细了 #include<bits/stdc++.h ...
- django unresolved template
参考:https://stackoverflow.com/questions/8487410/pycharm-django-1-3-static-url-in-templates-unresolved ...
- Django-models,继承AbstractUser类
1.UserInfo类继承了Django模型自带的User类,需要导入AbstractUser 2.然后在settings.py中配置
- LeetCode 3 :Min Stack
今天做了一道MinStack的题,深深的感到自己C++还完全没有学好!!! #include <iostream> #include <stack> using std::st ...