HDU 5831 Rikka with Parenthesis II (括號匹配)
Rikka with Parenthesis II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1609 Accepted Submission(s): 707
Problem Description
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:
Correct parentheses sequences can be defined recursively as follows:
1.The empty string "" is a correct sequence.
2.If "X" and "Y" are correct sequences, then "XY" (the concatenation of X and Y) is a correct sequence.
3.If "X" is a correct sequence, then "(X)" is a correct sequence.
Each correct parentheses sequence can be derived using the above rules.
Examples of correct parentheses sequences include "", "()", "()()()", "(()())", and "(((())))".
Now Yuta has a parentheses sequence S, and he wants Rikka to choose two different position i,j and swap Si,Sj.
Rikka likes correct parentheses sequence. So she wants to know if she can change S to a correct parentheses sequence after this operation.
It is too difficult for Rikka. Can you help her?
Correct parentheses sequences can be defined recursively as follows:
1.The empty string "" is a correct sequence.
2.If "X" and "Y" are correct sequences, then "XY" (the concatenation of X and Y) is a correct sequence.
3.If "X" is a correct sequence, then "(X)" is a correct sequence.
Each correct parentheses sequence can be derived using the above rules.
Examples of correct parentheses sequences include "", "()", "()()()", "(()())", and "(((())))".
Now Yuta has a parentheses sequence S, and he wants Rikka to choose two different position i,j and swap Si,Sj.
Rikka likes correct parentheses sequence. So she wants to know if she can change S to a correct parentheses sequence after this operation.
It is too difficult for Rikka. Can you help her?
Input
The first line contains a number t(1<=t<=1000), the number of the testcases. And there are no more then 10 testcases with n>100
For each testcase, the first line contains an integers n(1<=n<=100000), the length of S. And the second line contains a string of length S which only contains ‘(’ and ‘)’.
For each testcase, the first line contains an integers n(1<=n<=100000), the length of S. And the second line contains a string of length S which only contains ‘(’ and ‘)’.
Output
For each testcase, print "Yes" or "No" in a line.
Sample Input
3
4
())(
4
()()
6
)))(((
Sample Output
Yes
Yes
No
Hint
For the second sample input, Rikka can choose (1,3) or (2,4) to swap. But do nothing is not allowed.
Author
學軍中學
Source
題意:
給你一個括號串,讓你必須交換一次,判斷括號串是否匹配。必須!
POINT:
因為必須,所以單獨拎出n=2且為()這種情況,顯然是No。
我把左右括號不相等的情況也拎了出來,這樣感覺比較好寫。
其他情況:
如果一開始就是匹配的,交換後還是匹配。
如果一開始不匹配,就讓第一個使串不匹配的)和最後一個(交換。這樣還是沒有匹配的話,就是No,反之Yes。
cnt[]陣列是用來判斷匹配的。賦值規則為:遇(加1,遇)減1,有小於0的肯定就不匹配了。
交換一次“)” “(”號,那麼交換的區間內cnt[]全部加2 即多了一個左括號,少了一個右括號,總加2。若還有小於0的就是匹配不了。
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <math.h>
using namespace std;
#define ll long long
const int N = 100000+4;
char s[N];
int cnt[N];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int now=0;
int n;
cin>>n;
int flag=0;
cin>>s;
int you=999999;
int zuo;
int fz=0,fy=0;
for(int i=0;i<n;i++)
{
if(s[i]=='(')
{
now++;
zuo=i;
fz++;
}
else now--,fy++;
if(now<0&&!flag)
{
flag=1;
you=i;
}
cnt[i]=now;
}
int ans=1;
if(fz!=fy) ans=0;
for(int i=you;ans&&i<=zuo;i++)
{
if(cnt[i]+2<0)
{
ans=0;
break;
}
}
if(strlen(s)==2&&s[0]=='('&&s[1]==')') printf("No\n");
else if(!ans) printf("No\n");
else printf("Yes\n");
}
}
相關文章
- HDU5831(2016多校第八場)———Rikka with Parenthesis II(水題)
- HDU5425Rikka with Tree II(數學期望)
- 【棧】括號匹配
- HDU5424Rikka with Graph II(哈密頓圖判斷)
- interleave字串;及括號匹配分析字串
- 資料結構括號匹配問題資料結構
- 括號匹配;及找數字續分析
- 【DFS】HDU 5423 Rikka with Tree
- LeetCode 3: PairsOfParentheses (括號匹配問題)LeetCodeAI
- 括號匹配的檢驗問題(C++)C++
- POJ 2955-Brackets(括號匹配-區間DP)Racket
- 括號匹配檢驗 資料結構運用資料結構
- hdu 6415 Rikka with Nash EquilibriumUI
- UVA 673 括號的匹配——經典棧的應用
- #1280 : Rikka with Sequence II [meet int the middle]
- HDU 6415 Rikka with Nash Equilibrium (DP)UI
- 理解正規表示式中的括號 (),方括號 [] 和大括號 {}
- HDU6415:Rikka with Nash Equilibrium(dp)UI
- 【資料結構】棧的應用--括號的匹配(c++)資料結構C++
- 括號畫家
- HDU-6415 Rikka with Nash Equilibrium (DP/找規律)UI
- c++物件建立帶括號與無括號的區別C++物件
- JavaScript中圓括號()和方括號[]的一個特殊用法JavaScript
- Shell 括號總結
- Swift之花括號Swift
- 最長有效括號
- 【題解】括號序列
- 正則中括號點符號符號
- JSON 字串中的中括號和大括號區別詳解JSON字串
- JavaScript 函式呼叫時帶括號和不帶括號的區別JavaScript函式
- Matlab中的括號()[]{}Matlab
- 每日一題: 有效括號每日一題
- 20. 有效的括號
- 【leetcode】32. Longest Valid Parentheses 最長的有效匹配括號子串長度LeetCode
- [leetcode]有效的括號LeetCode
- [PHP字串]②--花括號{}的作用PHP字串
- ACM 括號配對問題ACM
- 22. 括號生成-c++C++