Test instructions: Look at the output to understand, violent enumeration, the maximum length of the string is 8, all lengths equal to 8 of the length is 8!=1x2x3x4x5x6x7x8=40320, the amount of data is smaller. Just the direction of the enumeration is rather weird, as in the case of a string of length equal to 3
A
Ab,ba
Abc,acb,cab
Bac,bca,cba
But the output is really not good output, in fact the output location is available to calculate.
Solution:
Make up a multi-fork tree, the root node is a, then the 1th layer has two children, the second layer has three children, the third layer has 4 children, has been generated downward, to the eighth floor, and then is to traverse this multi-fork tree.
AC Time, 80ms
2G of memory computer still can brush the question
#include <iostream> #include <stdio.h> #include <memory.h>using namespace Std;const int N = 8; #define Null nullstruct Node{char A[8];int al; node* Cp[8];int cl; Node () {memset (A, 0, sizeof (a)), AL = 0;memset (cp, 0, sizeof (CP)); cl = 0;};}; Char le[] = {' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h '};const int l = 8; node* root = null;void copy (node* root, node* Node, int index, char c) {int k = root->al + 1;int j = 0;for (int i = 0; i < K; i++) {if (i = = index) {node->a[node->al++] = c;continue;} node->a[node->al++] = root->a[j++];}} void Buildtree (node* root, int index, int n) {if (index = = n) return;for (int i = index; I >= 0; i--) {node* Node = new n Ode (); Copy (root, node, I, Le[index]); root->cp[root->cl++] = node;} for (int i = 0; i < root->cl; i++) Buildtree (Root->cp[i], index + 1, n);} void Blank (int n) {for (int i = 0; i < n; i++) cout << "";} void print (char a[], int l) {cout << a[0];for (int i = 1; i < L; i++) cout << "," <&Lt A[i];} void Dfs (node* root, int n, int index, int bs) {if (index + 1 = = N) {blank (BS); cout << "Writeln (";p rint (Root->a, RO Ot->al), cout << ")" << Endl;return; for (int i = 0; i < root->cl; i++) {blank (BS); if (i = = 0) {cout << "if";} else if (i = = root->cl-1) {cout << "else";} Else{cout << "Else if";} if (i = = 0) {cout << Root->cp[i]->a[index] << < << Root->cp[i]->a[index + 1]<< "<<" then ";} else if (i! = root->cl-1) {cout << root->cp[i]->a[index-i] << < << root->cp[i]-> A[index + 1-i] << "" << "then";} cout << Endl;dfs (Root->cp[i], N, index + 1, BS + 2);}} int main () {freopen ("C:\\users\\zzzzz\\desktop\\1.txt", "R", stdin); int casenum = 0;cin >> casenum;while ( casenum--) {int m;cin >> m;cout << program sort (input,output); << endl;cout << "var" << end L;print (Le, M); cout << ":" << "Integer;" << endl;cout << "Begin" << endl;int bs = 2;blank (BS); cout << "Readln (";p rint (Le, M); cou T << ");" << Endl;int index = 0;root = new Node (); root->a[root->al++] = Le[index];index++;buildtree (root , index, m);//cout << root->cl << endl;if (m = = 1) {blank (BS); cout << "Writeln (a)" << Endl;} Else{dfs (Root, M, 0, BS);} cout << "end." << endl;if (Casenum! = 0) {cout << Endl;}} return 0;}
uva-110-no sort for loop