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");
}
}
相關文章
- 【棧】括號匹配
- hdu 6415 Rikka with Nash EquilibriumUI
- HDU 6415 Rikka with Nash Equilibrium (DP)UI
- LeetCode 3: PairsOfParentheses (括號匹配問題)LeetCodeAI
- HDU6415:Rikka with Nash Equilibrium(dp)UI
- 棧和括號匹配,一文搞懂
- 資料結構括號匹配問題資料結構
- 括號匹配的檢驗問題(C++)C++
- 正規表示式(匹配第一個花括號)
- HDU-6415 Rikka with Nash Equilibrium (DP/找規律)UI
- HDU 1002 A + B Problem II
- 理解正規表示式中的括號 (),方括號 [] 和大括號 {}
- 2018 Multi-University Training Contest 9----hdu 6415 Rikka with Nash EquilibriumAIUI
- 【dp+組合數學】hdu 2018 多校第九場 1001 Rikka with Nash Equilibrium hdu 6415UI
- 括號畫家
- 有效的括號
- 【leetcode】32. Longest Valid Parentheses 最長的有效匹配括號子串長度LeetCode
- c++物件建立帶括號與無括號的區別C++物件
- {} 花括號的用法
- 最長有效括號
- [leetcode]有效的括號LeetCode
- 【題解】括號序列
- Shell 括號總結
- LeetCode有效的括號(Python)LeetCodePython
- 雙括號初始化
- 每日一題: 有效括號每日一題
- leetcode:遞迴:括號生成LeetCode遞迴
- 括號生成-LeetCode22LeetCode
- 20. 有效的括號
- LeetCode-022-括號生成LeetCode
- Leetcode——20. 有效的括號LeetCode
- 力扣 22. 括號生成力扣
- 22. 括號生成-c++C++
- Leetcode20. 有效的括號LeetCode
- 【LeetCode】 20.有效的括號LeetCode
- 【LeetCode-棧】有效的括號LeetCode
- [C++]括號使用小技巧C++
- LeetCode 20. 有效的括號LeetCode