日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【暴力】UVALive - 4882 - Parenthesis

發布時間:2025/5/22 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【暴力】UVALive - 4882 - Parenthesis 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

就不斷地掃整個序列,如果發現多余的括號就刪除。大概復雜度還是O(n2)左右。如何判斷不合法請詳見代碼。

?

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; }

轉載于:https://www.cnblogs.com/autsky-jadek/p/6291553.html

總結

以上是生活随笔為你收集整理的【暴力】UVALive - 4882 - Parenthesis的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。