现在的位置: 首页 > 综合 > 正文

南阳ACM2-括号配对

2014年02月07日 ⁄ 综合 ⁄ 共 768字 ⁄ 字号 评论关闭

/*描述
现在,有一行括号序列,请你检查这行括号是否配对。
输入
第一行输入一个数N(0<N<=100),表示有N组测试数据。后面的N行输入多组输入数据,每组输入数据都是一个字符串S(S的长度小于10000,且S不是空串),测试数据组数少于5组。数据保证S中只含有"[","]","(",")"四种字符
输出
每组输入数据的输出占一行,如果该字符串中所含的括号是配对的,则输出Yes,如果不配对则输出No
样例输入
3
[(])
(])
([[]()])
样例输出
No
No
Yes
*/

#include <iostream>
#include <cstring>
using namespace std;

const int N = 10001;

bool isMatch(char *p);

int main()
{
// freopen("1.txt", "r", stdin);
// freopen("2.txt", "w", stdout);
int n;
char ch[N] = {0};

cin>>n;
while(n--)
{
cin>>ch;
if (isMatch(ch))
{
cout<<"Yes"<<endl;
}
else
{
cout<<"No"<<endl;
}
}

return 0;
}

bool isMatch(char *p)
{
int length;
int flags = -1;
char temp[N] = {'@'};

length = strlen(p);
for (int i = 0; i < length; ++i)
{
if ((temp[flags]=='('&&p[i]==')') || (temp[flags]=='['&&p[i]==']'))
{
--flags;
}
else
{
++flags;
temp[flags] = p[i];
}
}
// cout<<flags<<endl;
if (flags==-1)
{
return true;
}

return false;

}

抱歉!评论已关闭.