题意:看输出就懂了,暴力枚举题,字符串最大长度是8,所有长度等于8的长度是8!=1x2x3x4x5x6x7x8=40320,数据量比较小的.只是枚举的方向比较怪异,如下,长度等于3的串

a

ab,ba

abc,acb,cab

bac,bca,cba

但是输出确实不好输出,事实上输出的位置是可用计算出来的.

解法:

组成一颗多叉树,根节点是a,那么第1层有俩个孩子,第二层有三个孩子,第三层有4个孩子,一直往下生成,到第八层,然后就是遍历这颗多叉树.

AC时间,80ms

2G内存的电脑还是能刷题的

#include <iostream>
#include <stdio.h>
#include<memory.h>
using namespace std; const int N = 8;
#define null NULL
struct 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 Node();
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 << "," << a[i]; }
void dfs(Node* root, int n, int index, int bs)
{
if (index + 1 == n)
{
blank(bs);
cout << "writeln(";
print(root->a, root->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" << endl;
print(le, m);
cout << " : " << "integer;" << endl;
cout << "begin" << endl;
int bs = 2;
blank(bs);
cout << "readln(";
print(le, m);
cout << ");" << 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-没有for循环的排序的更多相关文章

  1. UVA.10474 Where is the Marble ( 排序 二分查找 )

    UVA.10474 Where is the Marble ( 排序 二分查找 ) 题意分析 大水题一道.排序好找到第一个目标数字的位置,返回其下标即可.暴力可过,强行写了一发BS,发现错误百出.应了 ...

  2. Uva 110 - Meta-Loopless Sorts(!循环,回溯!)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  3. UVA 1386 - Cellular Automaton(循环矩阵)

    UVA 1386 - Cellular Automaton option=com_onlinejudge&Itemid=8&page=show_problem&category ...

  4. UVA 11039-Building designing【贪心+绝对值排序】

    UVA11039-Building designing Time limit: 3.000 seconds An architect wants to design a very high build ...

  5. for循环去重排序

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. lodash(二)对象+循环遍历+排序

    前言: lodash(一)中只是研究了array中的多种方法,接下来就是经常用到的循环遍历问题 过程: 1._.forEach(collection, [iteratee=_.identity], [ ...

  7. Python基础学习三 list-增删改查、切片、循环、排序

    一.list 增删改查 1.增加 方式一: stus = ['xiaohei','xiaobai','xiaohuang','cxdser'] stus.append('test001')#从最后面开 ...

  8. python学习第十二天列表的循环,排序,统计操作方法

    python列表最重要的列表的循环,任何有序列表离不开循环,列表的循环 for  in  range等关键词,还有列表排序,正序,倒序,还有列表每个元素的最大,最小,统计元素的个数等. 1,列表的循环 ...

  9. 只有一重循环的排序——侏儒排序(Gnome Sort)

    侏儒排序:从头(i=0)开始遍历元素,如果当前元素比前一个元素大(array[i]>array[i-1]),就把它跟前一个元素互换(Swap(a[i],a[i-1]))并继续检查它(i--),否 ...

  10. UVA 110 Meta-Loopless Sorts(输出挺麻烦的。。。)

     Meta-Loopless Sorts  Background Sorting holds an important place in computer science. Analyzing and ...

随机推荐

  1. Android 之WebView实现下拉刷新和其他相关刷新功能

    最近项目中需要用到WebView下拉刷新的功能,经过查找资料终于完成了此功能,现在拿出来和大家分享一下.希望对大家有所帮助. 效果如下图:   代码: activity.xml <?xml ve ...

  2. UVA-1343 The Rotation Game (IDA*)

    题目大意:数字1,2,3都有八个,求出最少的旋转次数使得图形中间八个数相同.旋转规则:对于每一长行或每一长列,每次旋转就是将数据向头的位置移动一位,头上的数放置到尾部.若次数相同,则找出字典序最小旋转 ...

  3. APP的六种loading加载样式,全在这...

    今天这篇文章是给大家分享的loading加载的设计,文章里面会有一些实例在这分享给大家! 大多数App都要与服务器进行数据的交换,App向服务器发出数据请求,服务器接收到请求之后向App传输相应数据, ...

  4. 043——VUE中组件之使用.sync修饰符与computed计算属性实现购物车原理

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. CF 272E Dima and Horses 染色,dfs 难度:2

    http://codeforces.com/problemset/problem/272/E 把仇恨关系想象为边, 因为度只能为0,1,2,3,所以有以下几种 0,1 直接放即可 2: 有(1,1), ...

  6. jsp登录页面,展示错误信息,刷新页面后错误依然存在解决方案

    在做登录页面的时候,通常使用form表单同步提交的方法进行提交的,也就是在form表单里去写action,如果登录失败,jsp通过jstl表达式获取错误信息展示在页面上,但是有一个问题就是,即使你刷新 ...

  7. Xilinx SDK使用教程

    本文参考 Xilinx SDK软件内置的教程,打开方法:打开SDK->Help->Cheet Sheets...->Xilinx SDK Tutorials,这里有6篇文档.本文详细 ...

  8. linux中~和/区别

    /是指根目录  就是所有目录最顶层的目录~指的是你当前用户的主目录  如果是root用户的话就是/root/目录    如果是其他用户的话就是/home/下以你用户名命名的用户 在linux里面,~/ ...

  9. [笔记] linux中的计划任务crontab

    不能拒绝进步 cron来源于希腊单词chronos(意为"时间"),指linux系统下一个自动执行指定任务的程序(计划任务). 1--crontab 的命令选项 #crontab ...

  10. Jenkins搭建.NET自动编译发布远程环境

    继上一篇文章Jenkins搭建.NET自动编译发布本地环境 发布到本地成功后,接下来配置发布到远程环境. Build配置——发布到远程 根据前面VS中发布项目,生成的CustomProfile2 来配 ...