大佬们,蒟蒻求助

P1241 括号序列

EarthGiao @ 2019-03-21 15:26:35

#include<iostream>
#include<stack>
#include<cstring>
#include<cstdio>
using namespace std;
char a[2000001];
int main()
{
    int n;
    stack<int>s;
    int flag=1,i,j;
    cin>>n;
    for(i=1;i<=n;++i)
    {
        scanf("%s",a);
        int l=strlen(a);
        for(j=0;j<l;++j)
        {
            if(a[j]=='('||a[j]=='{'||a[j]=='['||a[j]=='<')
            {
                s.push(a[j]);
            }
            else
            if(a[j]==')'&&!s.empty()&&s.top()=='(')
            {
                s.pop();
            }
            else
            if(a[j]=='>'&&!s.empty()&&s.top()=='<')
            {
                s.pop();
            }
            else
            if(a[j]=='}'&&!s.empty()&&s.top()=='{')
            {
                s.pop();
            }
            else
            if(a[j]==']'&&!s.empty()&&s.top()=='[')
            {
                s.pop();
            }
            else
            {
                flag=0;
                break;
            }
        }
        if(flag==1&&s.empty())
            cout<<"TRUE"<<endl;
        else
        {
            cout<<"FALSE"<<endl;
            if(!s.empty())
            {
                while(!s.empty())
                {
                    s.pop();
                }
            }
        }
        memset(a,0,sizeof(a));
    }
    return 0;
}

by dead_gun @ 2019-03-21 15:29:05

@EarthGiao 题目对不上


by EarthGiao @ 2019-03-21 15:30:19

哦哦哦,大佬们我找到错误的地方了我忘了吧flag再变回初始值1了,QWQ.


by dead_gun @ 2019-03-21 15:30:41

如果是codeVS上的话

正确代码:

#include<iostream>
#include<stack>
#include<cstring>
#include<cstdio>
using namespace std;
char a[2000001];
int main()
{
    int n;
    stack<int>s;
    int flag=1,i,j;
    cin>>n;
    for(i=1;i<=n;++i)
    {
        flag=1;
        scanf("%s",a);
        int l=strlen(a);
        for(j=0;j<l;++j)
        {
            if(a[j]=='('||a[j]=='{'||a[j]=='['||a[j]=='<')
            {
                s.push(a[j]);
            }
            else
            if(a[j]==')'&&!s.empty()&&s.top()=='(')
            {
                s.pop();
            }
            else
            if(a[j]=='>'&&!s.empty()&&s.top()=='<')
            {
                s.pop();
            }
            else
            if(a[j]=='}'&&!s.empty()&&s.top()=='{')
            {
                s.pop();
            }
            else
            if(a[j]==']'&&!s.empty()&&s.top()=='[')
            {
                s.pop();
            }
            else
            {
                flag=0;
                break;
            }
        }
        if(flag==1&&s.empty())
            cout<<"TRUE"<<endl;
        else
        {
            cout<<"FALSE"<<endl;
            if(!s.empty())
            {
                while(!s.empty())
                {
                    s.pop();
                }
            }
        }
        memset(a,0,sizeof(a));
    }
    return 0;
}

太阳留

@EarthGiao


by EarthGiao @ 2019-03-21 15:31:59

@dead_gun 大佬是codves上的,嗯嗯嗯


by yu__xuan @ 2019-03-21 16:18:40

@dead_gun 发现野生太阳


by 拈花卜笑 @ 2019-07-28 09:53:23

考古


|