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 ...
随机推荐
- MongoDB中的数据聚合工具Aggregate和Group
周煦辰 2016-01-16 来说说MongoDB中的数据聚合工具. Aggregate是MongoDB提供的众多工具中的比较重要的一个,类似于SQL语句中的GROUP BY.聚合工具可以让开发人员直 ...
- 最近公共祖先问题 LCA
2018-03-10 18:04:55 在图论和计算机科学中,最近公共祖先,LCA(Lowest Common Ancestor)是指在一个树或者有向无环图中同时拥有v和w作为后代的最深的节点. 计算 ...
- URI,URL与URN的区别
2017-11-13 16:51:49 URI = Universal Resource Identifier 统一资源标志符 URL = Universal Resource Locator 统一资 ...
- hdu1527威佐夫博弈
参考博客 https://hrbust-acm-team.gitbooks.io/acm-book/content/game_theory/wei_zuo_fu_bo_yi.html 满足 ,后手必胜 ...
- java并发编程:线程安全管理类--原子操作类--AtomicIntegerArray
1.类 AtomicIntegerArray
- 09_Git patch(补丁)操作
Git打补丁,补丁操作一般在多人开发时才会用到,单人本地开发一般用不到,没必要. 应用场景举例: 我把我的更改打成一个补丁发给你,你来合并到你的代码中 或者,在家里电脑开发提交后,打一个补丁,拿到 ...
- linux下redis的安装及配置启动
linux下redis的安装及配置启动 标签: redisnosql 2014-10-24 14:04 19732人阅读 评论(0) 收藏 举报 分类: 数据与性能(41) wget http:/ ...
- Ubuntu 16.04系统开机紫屏的解决办法
具体症状为卡在开机界面,按任何键都无反应. 网上查看了几篇文章 ,如下: 解决:ubuntu16.04启动时长时间停留在紫屏或跳文本的黑屏界面 Ubuntu16.04显卡驱动 电源管理 里面提到的开机 ...
- linux下部署tomcat服务器之安装tomcat
下载tomcat压缩包 apache-tomcat-7.0.82.tar.gz 在把包放到linux 的softwore文件夹下 自己选择文件夹 tar -zxvf apache-tomcat-7. ...
- vue.js 源代码学习笔记 ----- codegen.js
/* @flow */ import { genHandlers } from './events' import { baseWarn, pluckModuleFunction } from '.. ...