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;
}

  

时间: 2024-08-06 05:31:13

uva-110-没有for循环的排序的相关文章

UVA 1386 - Cellular Automaton(循环矩阵)

UVA 1386 - Cellular Automaton 题目链接 题意:给定一个n格的环,现在有个距离d,每次变化把环和他周围距离d以内的格子相加,结果mod m,问经过k次变换之后,环上的各个数字 思路:矩阵很好想,每个位置对应周围几个位置为1,其余位置为0,但是这个矩阵有500,有点大,直接n^3去求矩阵不太合适,然后观察发现这个矩阵是个循环矩阵,循环矩阵相乘的话,只需要保存一行即可,然后用n^2的时间就足够计算了 代码: #include <stdio.h> #include <

uva 10026 Shoemaker&#39;s Problem(贪心+排序)

虽然是个水题,但是在一些细节上wa了几次,好像不支持'\b'退格符号,我用在了输出空格那,结果wa了...白白 wa了几次...题意是看的题解..今天只写了两道题,速度有点慢,得加快了,以后得先认真读懂题目,题目读懂了 就相当于做出来一半然后仔细动脑想想,有想法了再敲,不能盲目的做题.另外,热烈祝贺今天c++ primer看到 了100页 思路: 这道题是让给的数据是每件工作需要做的天数和每耽误一天所需要的费用,让求一个序列使得付费最小,如果有相同答 案把字典树最小的输出...输出的是序号,该件

uva 10905 Children&#39;s Game(排序或者有点贪心)

今天配置vim没有成功,老是显示什么error,唉,其实之前成功过的,只不过是重装了dev,然后就变了,可能环境 变量的问题,但是我都改了的啊,以后再调吧... 这道题其实不是我想出来的看的题解,又看题解了...好吧,既然看了题解就得好好掌握才是.用到了我刚刚在 c++ primer里面学的string类,挺好用的,以后我准备写程序尽量用c++内容,多练练.. 又加深理解了qsort调用的cmp函数,它的两个参数其实都是要比较的元素的指针,比如这道题中的元素是string类 型,那么他们都是st

uva:10763 - Foreign Exchange(排序)

题目:10763 - Foreign Exchange 题目大意:给出每个同学想要的交换坐标 a, b 代表这位同学在位置a希望能和b位置的同学交换.要求每一位同学都能找到和他交换的交换生. 解题思路:把给定的原先给定的序列,交换前后位置后得到新的序列.如果这个新的序列和原来的序列相同就符合要求.因为  (a,b) (b, a)若是成对出现,那么前后交换后还是(b, a)(a,b). 代码: #include <stdio.h> #include <string.h> #inclu

UVa 872 - Ordering 输出全拓扑排序

本题要求输出全部拓扑排序的序列. 还好本题的数据量不是很大,限制在26个大写英文字母,故此可以使用递归法输出. 这个递归输出全部解在Leetcode很多这样的题目的,不小心的话,还是很难调试的. 总体考了递归和拓扑排序,还有判断是否可以拓扑排序-就是是否图有环. 考了三大知识点,难度还是有的.因为数据量不大,故此判断环可以使用一般递归方法,递归只需要注意细节就好了. #include <stdio.h> #include <vector> #include <string.h

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

题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=46  Meta-Loopless Sorts  Background Sorting holds an important place in computer science. Analyzing and implementing various so

UVa 110 没有循环的排序程序

题意:构造Pascal的排序程序.初看是写Pascal程序,不了解的以为会很难,但其实程序的大部分是固定的,直接printf就可以,主要在于写比较的if-else部分. 思路:看sample out可以大概知道程序的构成,其他部分直接输出,主要写比较的部分.比较的时候,可以看成两个集合,A是已排好序的,S是全集,cur是从左到右扫描S的当前位置.用递归写的,前半部分是当当前位置cur到达n时,即可以输出了:剩下的是如何填写当前位置cur以及维持A是已排好序的这个性质.将新元素S[cur]从A的最

UVA - 10305 - Ordering Tasks (拓扑排序!)

UVA - 10305 Ordering Tasks Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Problem F Ordering Tasks Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB John has n task

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

前言: lodash(一)中只是研究了array中的多种方法,接下来就是经常用到的循环遍历问题 过程: 1._.forEach(collection, [iteratee=_.identity], [thisArg])  遍历 _.forEach([22,33,11,55],function (value) {//若一个参数,返回的便是其value值 console.log(value);//22 33 11 55 }); _.forEach([22,33,11,55],function (va

uva 10602 Editor Nottoobad(字符串 + 排序)

uva 10602 Editor Nottoobad Company Macrohard has released it's new version of editor Nottoobad, which can understand a few voice commands. Unfortunately, there are only two voice commands that it can understand – "repeat the last word", "de