ACM UVa 算法题 #110 - Meta-Loopless Sort 的解法

题目的Link在这里: ACM UVa #110 - Meta-Loopless Sort

这道题的关键在于生成全排列的同时也需要确定如何进行比较可以得到此种全排列。生成全排列的方法有很多,但是有一种方法最适合此题:

假设我们获得了1~n的全排列,如何获得1~n+1的全排列?对于每个1~n的全排列x(1), x(2), ... x(n),可以看作该排列中有n+1个空位,即,<slot>, x(1), <slot>, x(2), <slot>, ...., x(n), <slot>,于是把x(n+1)分别放入这n+1个空位可以得到从x(1), ... x(n)生成的所有1~n+1的全排列。同时,放入某个空位的同时也就决定了x(n+1)和每个元素的大小关系:

因为有x(1), x(2), ..., x(n),因此有x(1) <= x(2) <= x(3) .... <= x(n),则

x(n) < x(n+1), 则插入生成的排列为x(1), x(2), ..., x(n), x(n+1)
否则,如果x(n-1) < x(n+1), 则插入生成的排列为x(1), x(2), ..., x(n-1), x(n+1), x(n)
....
否则,(此时x(n+1) < x(1)),因此排列为x(n+1), x(1), ..., x(n)

通过这个关系就很容易生成整个if语句了。

此外,整个if语句比较像一颗二叉树,每个分支对应着Then/Else子句,所以采用类似深度优先的方式遍历生成此if语句是最自然的方法:

代码如下:

//  
//  ACM UVa Problem #110
//   http://acm.uva.es/p/v1/110.html
//
//  Author:  ATField
//  Email:   atfield_zhang@hotmail.com
//

#include 
< iostream >
#include 
< vector >
#include 
< cstdlib >

using   namespace  std;

void  indent( int  n)
{
    
// output indent
    forint j = 0; j < n; ++j )
        cout 
<< "  ";
}


void  make_prog(  int  n,  int  end,  const  vector < int >   & seq )
{
    
if( n == end )
    
{
        
// we already come to the end
        
// just output the vector
        indent(n);

        cout 
<< "writeln(";

        
forint i = 0; i < n; ++i )
        
{
            
if( i > 0 )
                cout 
<< ",";
            cout 
<< char'a' + seq[i] );
        }


        cout 
<< ")" << endl;
    }

    
else
    
{
        
// big if statement
        forint i = n; i >= 0--i )
        
{
            indent(n);
            
if( i < n )
                cout 
<< "else ";
            
if( i > 0 )
                cout 
<< "if " << char'a' + seq[i - 1] ) << " < " << char'a' + n ) << " then";
            cout 
<< endl;

            vector
<int> new_seq = seq;
            new_seq.insert( new_seq.begin() 
+ i, n );
            make_prog( n 
+ 1, end, new_seq );
        }

    }

}



void  output_program( int  n)
{
    vector
<int> seq;
    seq.push_back(
0);           // 'a' is the initial sequence

    
//
    
// Output the begin part of the program
    
//
    cout << "program sort(input,output);" << endl
         
<< "var" << endl;
    
    
// var
    forint i = 0; i < n; ++i )
    
{
        
if( i > 0 )
            cout 
<< ",";
        cout 
<< char'a' + i );
    }

    cout 
<< " : integer;" << endl;
    cout 
<< "begin" << endl;

    
// readln
    cout << "  readln(";
    
forint i = 0; i < n; ++i )
    
{
        
if( i > 0 )
            cout 
<< ",";
        cout 
<< char'a' + i );
    }

    cout 
<< ");" << endl;

    
//
    
// Output the big if statement
    
//
    make_prog(1, n, seq);       // output the whole program from 1 to n

    cout 
<< "end." << endl;

}


int  main( int  argc,  char *  argv[])
{
    
int m;

    cin 
>> m;                       // total m programs to make

    
forint i = 0; i < m; ++i )
    
{
        
int n;

        cin 
>> n;

        output_program(n);
    }


    
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值