您的位置:首页 > 其它

括号配对问题————南阳OJ

2014-07-28 21:11 169 查看

通常,此问题主要用栈来进行,但在此通过数组,用数组的最后一位来模拟栈顶:梅毒如一个括号,若是左括号,则或者使置顶的最急迫的期待得以消解,或者是不合法的情况;若是左括号,则作为一个新的更急迫的期待压入栈中。

#include <iostream>

#include <string.h>

using namespace std;

int main()

{

int n,i,top=0;;

char ch[10000],s[5000];

cin >> n;

if (n<=0 || n>100)

return -1;

while (n--)

{

cin >> ch;

for (i=0;i<strlen(ch);i++)

{

if (ch[i] == '[' || ch[i] == '(')

s[top++]=ch[i];

else

{

if ((ch[i] == ')' && s[top-1] == '(') || (ch[i] ==']' && s[top-1] == '['))

top--;

else

{

cout << "No"<<endl;

break;

}

}

}

if (strlen(ch) != 0)

{

if (i==strlen(ch) && top==0)

cout << "Yes" <<endl;

}

else

n++;

top=0;

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: