#include <iostream>
#include <string>
using namespace std;
int main()
{
int t;
cin >> t;
while(t--)
{
string s;
cin >> s;
int num = 0;
while(s.length() >= 2 && num != s.length())
{
num = s.length();
for(int i = 0;i < s.length(); ++i)
if((s[i] == '(' && s[i + 1] == ')') || (s[i] == '[' && s[i + 1] == ']'))
s.erase(s.begin() + i, s.begin() + i + 2);
}
if(s.length() == 0)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
题目:Longest Regular Bracket Sequence
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
int dp[1234567];
char s[1234567];
int main()
{
gets(s);
int n = strlen(s);
vector<int> st;
memset(dp, 0, sizeof(dp));
for (int i = 0; i < n; i++)
{
if (s[i] == '(')
st.push_back(i);
else
{
if (!st.empty())
{
int he = st[st.size() - 1];
st.pop_back();
if (!he) dp[i] = i + 1;
else dp[i] = dp[he - 1] + i - he + 1;
}
}
}
int ans = 0, cnt = 1;
for (int i = 0; i < n; i++)
{
if (dp[i] == ans)
cnt++;
else if (dp[i] > ans)
{
ans = dp[i];
cnt = 1;
}
}
if (!ans) cnt = 1;
printf("%d %d\n", ans, cnt);
return 0;
}
#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int main()
{
string s;
int cur = 0 , ans = 0;
cin>>s;
for(int i = 0 ; i < s.length() ; i++)
{
if(s[i] == '(')
cur ++ , ans ++;
if(s[i] == ')' && cur > 0)
cur -- , ans ++;
}
cout<<ans - cur<<endl;
return 0;
}
#include <iostream>
#include <string.h>
#include <string>
const int N = 105;
bool is(char a,char b)
{
if(a=='(' && b==')') return true;
if(a=='[' && b==']') return true;
return false;
}
int main()
{
int t,i,j,k;
int dp[N][N];
std::string s;
std::cin>>t;
while(t--)
{
std::cin>>s;
memset(dp,0,sizeof(dp));
for(i=0;i<=s.length();i++)
dp[i][i]=1;
for(i=2;i<=s.length();i++)
{
for(j=i-1;j>=1;j--)
{
dp[j][i]=dp[j][i-1]+1;
for(k=j;k<i;k++)
{
if(is(s[k-1],s[i-1]))
dp[j][i]=std::min(dp[j][i],dp[j][k-1]+dp[k+1][i-1]);
}
}
}
std::cout<<dp[1][s.length()]<<std::endl;
}
return 0;
}