Meta-Loopless Sorts
Meta-Loopless Sorts |
Background
Sorting holds an important place in computer science. Analyzing and implementing various sorting algorithms forms an important part of the education of most computer scientists, and sorting accounts for a significant percentage of the world's computational resources. Sorting algorithms range from the bewilderingly popular Bubble sort, to Quicksort, to parallel sorting algorithms and sorting networks. In this problem you will be writing a program that creates a sorting program (a meta-sorter).
The Problem
The problem is to create several programs whose output is a standard Pascal program that sorts n numbers where n is the only input to the program you will write. The Pascal programs generated by your program must have the following properties:
- They must begin with program sort(input,output);
- They must declare storage for exactly ninteger variables. The names of the variables must come from the first n letters of the alphabet (a,b,c,d,e,f).
- A single readln statement must read in values for all the integer variables.
- Other than writeln statements, the only statements in the program are if then else statements. The boolean conditional for each if statement must consist of one strict inequality (either < or >) of two integer variables. Exactly n!writeln statements must appear in the program.
- Exactly three semi-colons must appear in the programs
- after the program header: program sort(input,output);
- after the variable declaration: ...: integer;
- after the readln statement: readln(...);
- No redundant comparisons of integer variables should be made. For example, during program execution, once it is determined that a < b, variables a and b should not be compared again.
- Every writeln statement must appear on a line by itself.
- The programs must compile. Executing the program with input consisting of any arrangement of any n distinct integer values should result in the input values being printed in sorted order.
For those unfamiliar with Pascal syntax, the example at the end of this problem completely defines the small subset of Pascal needed.
The Input
The input consist on a number in the first line indicating the number M of programs to make, followed by a blank line. Then there are M test cases, each one consisting on a single integer n on a line by itself with 1![$ \leq$](http://uva.onlinejudge.org/external/1/110img1.png)
![$ \leq$](http://uva.onlinejudge.org/external/1/110img1.png)
The Output
The output is M compilable standard Pascal programs meeting the criteria specified above. Print a blank line between two consecutive programs.
Sample Input
1 3
Sample Output
program sort(input,output); var a,b,c : integer; begin readln(a,b,c); if a < b then if b < c then writeln(a,b,c) else if a < c then writeln(a,c,b) else writeln(c,a,b) else if a < c then writeln(b,a,c) else if b < c then writeln(b,c,a) else writeln(c,b,a) end.
题目大意:
输入一个数字代表有多少个字母。要求输出一个排序程序。(前面的空格可以忽视)
解析:
这道题的关键在于生成全排列的同时也需要确定如何进行比较可以得到此种全排列。
生成全排列的方法有很多,但是有一种方法最适合此题:
假设我们获得了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]
#include <stdio.h>
#include <string>
#include <string.h>
using namespace std;
const int N = 20;
int n;
const char alpha[] = "abcdefghij";
string str = "";
int cnt;
void dfs(int cur) {
if(cur >= n) {
printf("writeln(");
for(int i = 0; i < n-1; i++) {
printf("%c,",str[i]);
}
printf("%c)\n",str[n-1]);
return ;
}
//从尾部插入
for(int i = str.size() - 1; i >= 0; i--) {
if( i != str.size() -1) {
printf("else ");
}
printf("if %c < %c then\n",str[i],alpha[cur]);
str.insert(i+1,1,alpha[cur]);
dfs(cur+1);
str.erase(i+1,1);
}
//插入到最前面
if(cnt) {
printf("else\n");
}
cnt++;
str.insert(0,1,alpha[cur]);
dfs(cur+1);
str.erase(0,1);
}
int main() {
int m;
while(scanf("%d",&m) != EOF) {
while(m--) {
scanf("%d",&n);
//output
printf("program sort(input,output);\n");
printf("var\n");
for(int i = 0; i < n-1; i++) {
printf("%c,",alpha[i]);
}
printf("%c : integer;\n",alpha[n-1]);
printf("begin\n");
printf("readln(");
for(int i = 0; i < n-1; i++) {
printf("%c,",alpha[i]);
}
printf("%c);\n",alpha[n-1]);
cnt = 0;
dfs(0);
printf("end.\n");
if(m) {
printf("\n");
}
}
}
return 0;
}