uva-110-没有for循环的排序
题意:看输出就懂了,暴力枚举题,字符串最大长度是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循环的排序的更多相关文章
- UVA.10474 Where is the Marble ( 排序 二分查找 )
UVA.10474 Where is the Marble ( 排序 二分查找 ) 题意分析 大水题一道.排序好找到第一个目标数字的位置,返回其下标即可.暴力可过,强行写了一发BS,发现错误百出.应了 ...
- Uva 110 - Meta-Loopless Sorts(!循环,回溯!)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- UVA 1386 - Cellular Automaton(循环矩阵)
UVA 1386 - Cellular Automaton option=com_onlinejudge&Itemid=8&page=show_problem&category ...
- UVA 11039-Building designing【贪心+绝对值排序】
UVA11039-Building designing Time limit: 3.000 seconds An architect wants to design a very high build ...
- for循环去重排序
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- lodash(二)对象+循环遍历+排序
前言: lodash(一)中只是研究了array中的多种方法,接下来就是经常用到的循环遍历问题 过程: 1._.forEach(collection, [iteratee=_.identity], [ ...
- Python基础学习三 list-增删改查、切片、循环、排序
一.list 增删改查 1.增加 方式一: stus = ['xiaohei','xiaobai','xiaohuang','cxdser'] stus.append('test001')#从最后面开 ...
- python学习第十二天列表的循环,排序,统计操作方法
python列表最重要的列表的循环,任何有序列表离不开循环,列表的循环 for in range等关键词,还有列表排序,正序,倒序,还有列表每个元素的最大,最小,统计元素的个数等. 1,列表的循环 ...
- 只有一重循环的排序——侏儒排序(Gnome Sort)
侏儒排序:从头(i=0)开始遍历元素,如果当前元素比前一个元素大(array[i]>array[i-1]),就把它跟前一个元素互换(Swap(a[i],a[i-1]))并继续检查它(i--),否 ...
- UVA 110 Meta-Loopless Sorts(输出挺麻烦的。。。)
Meta-Loopless Sorts Background Sorting holds an important place in computer science. Analyzing and ...
随机推荐
- kue
尝试着看英文的文档,你发现他其实并不难. https://www.npmjs.com/package/kue
- TinyURL
2018-03-09 15:19:04 TinyURL,短地址,或者叫短链接,指的是一种互联网上的技术与服务.此服务可以提供一个非常短小的URL以代替原来的可能较长的URL,将长的URL地址缩短. 用 ...
- ArrayList相关问题
设定初始化容量new一个list List<String> list = new ArrayList<>(10); list.add("s"); 进入构造方 ...
- org.springframework.beans.factory.BeanCreationException: sqlSessionFactory
sqlSessionFactory实例化错误 pom默认导入的jar包中存在低版本,导致实例化sqlSessionFactory错误,删除此jar包即可
- Docker环境准备-安装Ubuntu
***微信扫一扫,关注“python测试开发圈”,了解更多测试教程!***
- bzoj2134
题解: 每一题对的概率为min(a[i],a[i+1])/a[i]/a[i+1]; 即可 代码: #include<bits/stdc++.h> using namespace std; ...
- Docker ENTRYPOINT
entrypoint: 在启动镜像的时候会执行这个命令下的脚本,在docker run 和docker start情况下都会触发. 好比这个脚本是对某一个文件追加数据,每次start的时候都会追加,文 ...
- Beta发布
作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2408] 视频展示 链接[https://www.cnblogs.com/erc ...
- 20155211 2016-2017-2 《Java程序设计》第八周学习总结
20155211 2016-2017-2 <Java程序设计>第八周学习总结 教材学习内容总结 第十四章 NIO与NIO2 NIO使用频道(channel)来衔接数据节点,对数据区的标记提 ...
- 20155313 2016-2017-2 《Java程序设计》第八周学习总结
20155313 2016-2017-2 <Java程序设计>第八周学习总结 教材内容学习 十四章 NIO与NIO2 1.认识NIO NIO使用频道(Channel)来衔接数据节点,在处理 ...