寻找合法字符串

给出一个正整数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] = '\0';
        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;
}

 

posted @ 2019-02-24 21:19  strawqqhat  阅读(181)  评论(0)    收藏  举报
#home h1{ font-size:45px; } body{ background-image: url("放你的背景图链接"); background-position: initial; background-size: cover; background-repeat: no-repeat; background-attachment: fixed; background-origin: initial; background-clip: initial; height:100%; width:100%; } #home{ opacity:0.7; } .wall{ position: fixed; top: 0; left: 0; bottom: 0; right: 0; } div#midground{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -1; -webkit-animation: cc 200s linear infinite; -moz-animation: cc 200s linear infinite; -o-animation: cc 200s linear infinite; animation: cc 200s linear infinite; } div#foreground{ background: url("https://i.postimg.cc/z3jZZD1B/foreground.png"); z-index: -2; -webkit-animation: cc 253s linear infinite; -o-animation: cc 253s linear infinite; -moz-animation: cc 253s linear infinite; animation: cc 253s linear infinite; } div#top{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -4; -webkit-animation: da 200s linear infinite; -o-animation: da 200s linear infinite; animation: da 200s linear infinite; } @-webkit-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-o-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-moz-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @keyframes cc { 0%{ background-position: 0 0; } 100%{ background-position: 600% 0; } } @keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-webkit-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-moz-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-ms-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } }