Solution to a problem [P1706] full array problem

Provide an approach based on non-search ranking


Ideas:

The answer array half,
the variable \ (nn \) is bounded by
the first half of the ordered area, the second half of disordered region
of disordered area sorted according to the dictionary order

Each recursion,
all the elements taken from the disordered loop region,
and the ordered regions was added
after a shift of boundaries and

After re-disordered regions of the
sort in lexicographic order,
then the changed array as a parameter
passed to the next layer recursion

When the region is equal to the length ordered \ (L \) time,
they found a set of solutions, the output can be


Attach Code:


#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
int l; 
using namespace std;
void p(int nn,char b[10])        //将字符串分为有序区和无序区,以nn为界 
{
    for(int i=nn;i<=l;i++)       //从分界线上开始,依次枚举无序区元素 
      {
        char c[10];              //储存当前的 字符串 
        for(int k=0;k<=9;k++)
          c[k]=b[k];
        swap(c[nn],c[i]);        //令c[nn]等于找到的字符 
        if(nn+1<=l)              //边界小于l,则重排序无序区 
          sort(c+(nn+1),c+l+1);
        if(nn==l)                //若到达尾部,则输出 
          {
            for(int j=0;j<=l;j++)
              printf("%5c",c[j]);//输出坑点 
            printf("\n");
          }
        else
          p(nn+1,c);             //继续找下一个
      }
}
char a[10];
int b;
int main()
{
    cin>>b;    
    for(int i=0;i<b;i++)//初始化答案数组 
      a[i]=i+'1'; 
    l=strlen(a)-1;
    p(0,a);  
}

Guess you like

Origin www.cnblogs.com/luckyblock/p/11456395.html