hdu5831 Rikka with Parenthesis II
生活随笔
收集整理的這篇文章主要介紹了
hdu5831 Rikka with Parenthesis II
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://acm.hdu.edu.cn/showproblem.php?pid=5831
又是迷之水題,你說不難吧,悶聲發大財這是墜吼的,你說難吧,會不會有一種欽定的感覺
給出一個括號化序列問能不能通過一次必須的交換兩個位置上的括號變成合法的。
顯然先要用stack搞一搞,但是這里要求必須交換。于是分一下情況,一種是已經合法的,那么顯然只要n>2一定依然可以合法,一種是不合法的,那么剩下的形式一定是.....))))((((....,于是)(,))((合法,其余不合法。
做的時候沒有討論全))((
#include<cstdio> #include<cstring> #include<cstdlib> #include<vector> #include<algorithm> #include<functional> #include<iostream> #include<cmath> #include<string> #include<cctype> #include<stack> #include<queue> #include<set> #include<sstream> #include<map> using namespace std; #define For(i,k,n) for(int i=k;i<=n;i++) #define ForD(i,k,n) for(int i=n;i>=k;i--) #define Lson (u<<1) #define Rson ((u<<1)+1) #define MEM(a) memset(a,0,sizeof(a)); #define NEG(a) memset(a,-1,sizeof(a)); #define FILL(a) memset(a,0x3f,sizeof(a)); #define INF 0x3f3f3f3f #define LLINF 0x3f3f3f3f3f3f3f3f #define ll long long #define printbin(b,a){int tmp=a;string s;do{s+=tmp%2+'0';tmp/=2;}while(tmp);reverse(s.begin(),s.end());cout<<"bin "<<b<<"="<<s<<endl;} #define printarr(i,a,f,b) {For(i,f,b) cout<<a[i]<<" "; printf("\n");} #define ptf printf("fuck\n") #define pt(a) cout<<a<<endl #define flgprint(flg,a,b) {if(flg) printf("%s\n",a);else printf("%s\n",b);} #define fp freopen("in.txt","r",stdin)#define maxn 200000 int num; char c; char buf[maxn]; int s[maxn],sz; int main() {int T;scanf("%d",&T);while(T--){sz=0;int flg=0;int n;scanf("%d",&n);getchar();gets(buf+1);int cnt=0;For(i,1,n){c=buf[i];if(c=='(') num=1;else num=0;if(sz>0&&s[sz-1]==1&&num==0){sz--;}else{s[sz++]=num;if(num) cnt++;}}if(n-cnt!=cnt){puts("No");}else if(sz==0){if(flg||n>2){puts("Yes");}else puts("No");}else if(sz==2){int a=s[sz-1];int b=s[sz-2];if(a==1&&b==0) flg=1;if(flg==1){puts("Yes");}else puts("No");}else if(sz==4){int a=s[sz-1];int b=s[sz-2];int c=s[sz-3];int d=s[sz-4];if(a==1&&b==1&&c==0&&d==0) flg=1;if(flg==1){puts("Yes");}else puts("No");}else puts("No");}return 0; } View Code?
轉載于:https://www.cnblogs.com/diang/p/5766540.html
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的hdu5831 Rikka with Parenthesis II的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js中的prototype的理解
- 下一篇: linux驱动之I2C