UVA - 12335 Lexicographic Order (第k大排列)
来源:互联网 发布:阿里云虚拟主机代理 编辑:程序博客网 时间:2024/04/29 00:01
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 thek-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 integerk. 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 ofk 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大排列)
- lightoj1060(求字符串第k大排列)组合数学
- CSU 1563 Lexicography(全排列第K大)
- Problem 24 Lexicographic permutations (全排列)
- Problem 24 Lexicographic permutations (全排列)
- 找第k大的东西(密码,uva 1262)
- 求两个排序数组的第K大元素--求全排列的第K大元素
- 求排列中第k大的数--逆康托展开
- 【第K排列问题】
- 第k个排列
- 排列哈希、反哈希(求一个排列是所有排列中的第K个排列)
- 第K(大)小数
- UVA 11525 Permutation(线段树第K大数字问题)
- UVA.1262 Password ( dfs求字典序第k大 )
- 第k个排列数
- LintCode-第k个排列
- LinkCode 第k个排列
- 第k个排列-LintCode
- iOS NSString 和NSData 转换
- 1044. Shopping in Mars (25)
- android开发环境搭建
- unity3D物体的旋转和缩放
- block及arc中注意事项
- UVA - 12335 Lexicographic Order (第k大排列)
- oracle
- 键盘控制div移动
- [笔记] Android WebView使用方法
- structs2中interceptor(拦截器)的种类
- Linux scp 命令使用详解
- eclipse插件spket安装
- 关于各种码--ASCII--Unicode--UTF-8/16/32
- renderlayerbacking