zoukankan      html  css  js  c++  java
  • 【暴力】UVALive

    就不断地扫整个序列,如果发现多余的括号就删除。大概复杂度还是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;
    }
  • 相关阅读:
    Jmeter(四十一)分布式测试(转!)
    Jmeter(四十)BeanShell范例
    Jmeter(三十九)User.Properties定义全局变量
    Jmeter(三十八)Jmeter Question 之 ‘批量执行SQL语句’
    接口测试“八重天”---HttpClient
    常见的性能测试误区
    UI“三重天”之appium(一)
    Jmeter(三十七)源码导入IDE(转!)
    UI“三重天”之selenium--常用API和问题处理(三)
    kotlin 1.3
  • 原文地址:https://www.cnblogs.com/autsky-jadek/p/6291553.html
Copyright © 2011-2022 走看看