zoukankan      html  css  js  c++  java
  • 寻找合法字符串

    给出一个正整数n,请给出所有的包含n个'('和n个')'的字符串,使得'('和')'可以完全匹配。

    例如:

    '(())()','()()()' 都是合法的;

    '())()('是不合法的。

    请按照__字典序__给出所有合法的字符串。

    输入描述:

    输入为1个正整数
    

    输出描述:

    输出为所有合法的字符串,用英文逗号隔开

    示例1

    输入

    2

    输出

    (()),()()
    #include<stdio.h>
    #include<iostream>
    using namespace std;
    #define MAX 50
    bool start = true;
    void helpcore(char *array,int pos,int NumPre,int NumPost){
        if(NumPre>NumPost)
            return ;
        if(NumPre==0){
            while(NumPost){
                array[pos++]=')';
                NumPost--;
            }
            array[pos] = '';
            if(start){
                printf("%s",array);
                start=false;
            }
            else
                printf(",%s",array);
        }
        else{
            if(NumPre==NumPost){
                array[pos]='(';
                helpcore(array,pos+1,NumPre-1,NumPost);
            }
            else{
                array[pos]='(';
                helpcore(array,pos+1,NumPre-1,NumPost);
                array[pos]=')';
                helpcore(array,pos+1,NumPre,NumPost-1);
            }
        }
    
    }
    int main()
    {
        char array[MAX]={0};
        int n;
        cin>>n;
        helpcore(array,0,n,n);
        return 0;
    }
    
  • 相关阅读:
    bzoj 3747: [POI2015]Kinoman
    bzoj 3123: [Sdoi2013]森林
    bzoj 1901: Zju2112 Dynamic Rankings
    poj 1741 Tree
    bzoj 2152: 聪聪可可
    bzoj 2599: [IOI2011]Race
    bzoj 3697: 采药人的路径
    bzoj 2728: [HNOI2012]与非
    bzoj 2115: [Wc2011] Xor
    bzoj 3143: [Hnoi2013]游走
  • 原文地址:https://www.cnblogs.com/strawqqhat/p/10602255.html
Copyright © 2011-2022 走看看