UVA - 12335 Lexicographic Order (第k大排列)

Description


A


Lexicographic Order

Input: Standard Input

Output: Standard Output


The alphabet of a certain alien language consists of n distinct symbols. The symbols are like the letters of English alphabet but their ordering is different. You want to know the original order of the symbols in that particular alphabet.
You have a string consists of all the letters of that alphabet and you know that this is the
k-th (1 based) lexicographic permutation of these symbols. You have to arrange these symbols in lexicographic order of that language.

Input

The first line of input will contain an integer T (T ≤ 5000) which denotes the number of test cases.

Each of the following T lines contains a string s and an integer
k. The string will be of length n (1 ≤ n ≤ 20) and will consist of lowercase letters only. All the letters in the string will be distinct. The value of
k will be in the range (1 ≤ k ≤ n!).

Output

For each line of input output the case number and a string which contains the letters in lexicographic order in that language.

Sample Input                              Output for Sample Input


3

bdac 11

abcd 5

hjbrl 120


Case 1: abcd

Case 2: acdb

Case 3: lrbjh

题意:求第n大的排列

思路:举个简单的例子:序列:1234,求第11个排列,假设我们确定了第一个,那么剩下3个的排列数是3!,因为12是大于6的,所以我们不能放1放到第一个,当我们放2的时候,这个数能有的排列会到12<=12,所以我们就能确定第一个是2了,依次类推

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long ll;
using namespace std;
const int maxn = 30;

char str[maxn], ans[maxn];
int n, vis[maxn], ind[maxn]; 

ll cal(int x) {
	ll tmp = 1;
	for (int i = 2; i <= x; i++)
		tmp *= i;
	return tmp;
}

void dfs(int cur , ll dir) {
	if (cur == n)
		return;
	ll tmp = cal(n-cur-1);
	for (int i = 0; i < n; i++) {
		if (vis[i])
			continue;
		if (dir > tmp)
			dir -= tmp;
		else {
			vis[i] = 1;
			ind[cur] = i;
			dfs(cur+1, dir);
			return;
		}

	}
}

int main() {
	ll x;
	int t, cas = 1;
	scanf("%d", &t);
	while (t--) {
		scanf("%s%lld", str, &x);
		n = strlen(str);
		memset(vis, 0, sizeof(vis));
		dfs(0, x);
		printf("Case %d: ", cas++);
		for (int i = 0; i < n; i++)
			ans[ind[i]] = str[i];
		for (int i = 0; i < n; i++)
			printf("%c", ans[i]);
		printf("\n");
	}
	return 0;
}

UVA - 12335 Lexicographic Order (第k大排列)

时间: 2024-10-06 12:18:38

UVA - 12335 Lexicographic Order (第k大排列)的相关文章

ligh1060(求字符串第k大排列)组合数学

题意:求给定字符串(有重复字符)第k大排列. 解法:先判断字符串的所有排列是否够k个.然后从左向右每一位每一位确定.简单的组合数学. 代码: /**************************************************** * author:xiefubao *******************************************************/ #pragma comment(linker, "/STACK:102400000,10240000

Permutation UVA - 11525(值域树状数组,树状数组区间第k大(离线),log方,log)

Permutation UVA - 11525 看康托展开 题目给出的式子(n=s[1]*(k-1)!+s[2]*(k-2)!+...+s[k]*0!)非常像逆康托展开(将n个数的所有排列按字典序排序,并将所有排列编号(从0开始),给出排列的编号得到对应排列)用到的式子.可以想到用逆康托展开的方法.但是需要一些变化: for(i=n;i>=1;i--) { s[i-1]+=s[i]/(n-i+1); s[i]%=(n-i+1); } 例如:n=3时,3=0*2!+0*1!+3*0!应该变为3=1

LeetCode 笔记21 生成第k个排列

题目是这样的: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3): "123" "132" "213" "231" "312"

Coursera Algorithms week3 快速排序 练习测验: Selection in two sorted arrays(从两个有序数组中寻找第K大元素)

题目原文 Selection in two sorted arrays. Given two sorted arrays a[] and b[], of sizes n1 and n2, respectively, design an algorithm to find the kth largest key. The order  of growth of the worst case running time of your algorithm should be logn, where n

poj_1037 动态规划+字典序第k大

题目大意 给定n个数字,规定一种 cute 排序:序列中的数字大小为严格的波浪形,即 a[0] > a[1] < a[2] > a[3] < .... 或者 a[0] < a[1] > a[2] < a[3] ......对于N个数字来说,可以构成多个cute序列,这些序列按照字典序进行排序,求出第k个序列. 题目分析 一.求字典序的第i个排列 直接一位一位枚举答案!从前到后枚举求得每一位:枚举一位时,计算在这样的前缀下,后面的总的排列数.如果严格小于总编号,则该

HDU 5249 离线树状数组求第k大+离散化

KPI Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1160    Accepted Submission(s): 488 Problem Description 你工作以后, KPI 就是你的全部了. 我开发了一个服务,取得了很大的知名度.数十亿的请求被推到一个大管道后同时服务从管头拉取请求.让我们来定义每个请求都有一个重要值.我的

POJ2104 K-th Number (子区间内第k大的数字)【划分树算法模板应用】

K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 40920   Accepted: 13367 Case Time Limit: 2000MS Description You are working for Macrohard company in data structures department. After failing your previous task about key inse

POJ 2014.K-th Number 区间第k大 (归并树)

K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 57543   Accepted: 19893 Case Time Limit: 2000MS Description You are working for Macrohard company in data structures department. After failing your previous task about key inse

Poj 2104区间第k大(归并树)

题目链接 K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 36890 Accepted: 11860 Case Time Limit: 2000MS Description You are working for Macrohard company in data structures department. After failing your previous task about key ins