您的位置:首页 > 其它

NYoj+stack使用++runtime error概念基础知识不牢细节出错

2015-10-10 17:17 225 查看
点击打开链接
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string.h>
#include<string>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;
int main()
{
int N=0,i=0;
string str;
stack<char>s;
scanf("%d",&N);
while(N--)
{
cin>>str;
int len=str.length();
for(i=0;i<len;i++)
{
if(str[i]=='['||str[i]=='(')
{
s.push(str[i]);
}
else if(str[i]==']')
{
if(!s.empty()&&s.top()=='[')///一定要先用empty然后才能用top啊
{
s.pop();
}
else
{
printf("No\n");
break;
}
}
else if(str[i]==')')
{
if(!s.empty()&&s.top()=='(')///一定要有先判断是不是empty,才能用top啊
{
s.pop();
}
else
{
printf("No\n");
break;
}
}
}
if(i==len&&s.empty())
{
printf("Yes\n");
}
else if(i==len&&!s.empty())
{
printf("No\n");
}
while(!s.empty()) s.pop();///一定要清空啊
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: