zoukankan      html  css  js  c++  java
  • nyist 2 括号配对问题

    括号配对问题

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

    ******************************************************

    yhy

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


    int main()
    {
    int n,i,bz;
    char s[10008];
    stack <char> my;

    cin>>n;
    gets(s);
    while (n--)

    {
    gets(s);
    for (i=0;i<strlen(s);i++)
    {

    if ( s[i]== ')')
    if ( !my.empty() && my.top()=='(' )
    {

    my.pop();
    continue;

    }
    if ( s[i]== ']' )
    if ( !my.empty() && my.top()=='[' )
    {
    my.pop();
    continue;
    }
    my.push(s[i]);
    }
    if ( my.empty() ) cout<<"Yes ";
    else cout<<"No ";

    while (!my.empty()) my.pop();

    }
    return 0;
    }

     

    ********************************************************************************

    #include<stdio.h>
    #include<stack>
    using namespace std;
    stack< char >s;
    bool mate(char a,char b)
    {
    return a=='('&& b==')' || a=='['&& b==']';
    }
    int main()
    {
    int T,i;char str[10010];
    scanf("%d",&T);
    getchar();
    while(T--)
    {
    gets(str);
    // puts(str);
    while(!s.empty()) s.pop();
    for(i=0;str[i]!='';i++)
    {
    if(s.empty() || !mate( s.top(),str[i]))
    s.push(str[i]);
    else s.pop();
    }
    if(s.empty())
    printf("Yes ");
    else printf("No ");
    }
    return 0;
    }

    #include<iostream>
    using namespace std;
    #include<stack>
    #include<cstring>
    #include<cmath>
    int main()
    {
    stack<char>s;
    char a[100];
    int i,N;
    scanf("%d ",&N);
    while(N--)
    {
    gets(a);
    {
    int k=1;

    for(i=0;i<strlen(a);i++)
    {
    if(k==0) break;
    if(a[i]=='('||a[i]=='[') s.push(a[i]);
    if(a[i]==']'||a[i]==')')
    {
    if(s.empty())
    k=0;
    else
    { if(abs(s.top()-a[i])==1||abs(s.top()-a[i])==2) s.pop();
    else k=0;
    }
    }
    }

    if(k==1&&s.empty()) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    while(!s.empty()) s.pop();
    }
    }

    return 0;
    }


    #include<iostream>
    using namespace std;
    #include<stack>
    #include<cstring>
    #include<cmath>
    int main()
    {
    stack<char>s;
    char a[100];
    int i;
    while(gets(a))
    {
    int k=1;

    for(i=0;i<strlen(a);i++)
    {
    if(k==0) break;
    if(a[i]=='('||a[i]=='[') s.push(a[i]);
    if(a[i]==']'||a[i]==')')
    {
    if(s.empty())
    k=0;
    else
    { if(abs(s.top()-a[i])==1||abs(s.top()-a[i])==2) s.pop();
    else k=0;
    }
    }
    }

    if(k==1&&s.empty()) cout<<"yes"<<endl;
    else cout<<"no"<<endl;
    while(!s.empty()) s.pop();
    }
    return 0;
    }

  • 相关阅读:
    版本回退
    时光机穿梭
    创建版本库
    安装Git
    Git简介
    Nexus私服安装
    eclipse中创建MAVEN-web项目
    AsyncTask的使用
    在子线程中更新UI,只能使用Handler
    使用VideoView播放视频
  • 原文地址:https://www.cnblogs.com/2014acm/p/3901461.html
Copyright © 2011-2022 走看看