LightOJ - 1060 nth Permutation(计数)
来源:互联网 发布:淘宝店纳亮索尼怎么样 编辑:程序博客网 时间:2024/06/10 22:09
题目大意:给你一个字符串,问这个字符串的字典序排第n的字符串是什么
解题思路:计数问题,依此判断每一位即可
#include <cstdio>#include <cstring>using namespace std;typedef long long LL;const int N = 30;LL A[N];int cnt[N];char str[N], ans[N];LL k;int cas = 1, len;void start() { A[0] = A[1] = 1; for (int i = 2; i < 21; i++) A[i] = A[i - 1] * i;}LL Calculate() { LL t1 = 0, t2 = 1; for (int i = 0; i < 26; i++) { t1 += cnt[i]; t2 *= A[cnt[i]]; } return A[t1] / t2;}bool dfs(int pos, int cur, LL left) { if (pos >= len) return true; if (cnt[cur] == 0) return dfs(pos, cur + 1, left); cnt[cur]--; LL t = Calculate(); if (t >= left) { ans[pos] = 'a' + cur; return dfs(pos + 1, 0, left); } else { cnt[cur]++; return dfs(pos, cur + 1, left - t); }}void solve() { scanf("%s%lld", str, &k); len = strlen(str); memset(cnt, 0, sizeof(cnt)); for (int i = 0; i < len; i++) cnt[str[i] - 'a']++; if (Calculate() < k) printf("Case %d: Impossible\n", cas++); else { ans[len] = '\0'; dfs(0, 0, k); printf("Case %d: %s\n", cas++, ans); }}int main() { start(); int test; scanf("%d", &test); while (test--) solve(); return 0;}
0 0
- LightOJ - 1060 nth Permutation(计数)
- LightOJ 1060 - nth Permutation(dp)
- lightoj 1060 - nth Permutation 组合数学
- LightOJ 1060 - nth Permutation (逆康托展开的思想)
- lightoj 1060 - nth Permutation(逆康拓展开)
- LightOJ 1060nth Permutation(组合数--k大字典序)
- Light oj 1060 - nth Permutation(dfs 计数)
- Light OJ 1060 nth Permutation (状压DP)
- Lightoj 1122【计数DP】
- LightOJ 1005 - Rooks(计数)
- LightOJ 1096 nth Term (构造矩阵)
- 计数dp-hdu-3664-Permutation Counting
- HDU-6044 Limited Permutation(计数)
- CS 400 Restricted Permutation 排列,DP计数
- lightoj 1231 1232 1233 DP 背包计数
- LightOJ 1173 The Vindictive Coach (DP 计数)
- lightoj 1294 - Positive Negative Sign 【基础计数】
- lightoj 1245 - Harmonic Number (II) 【数学 计数】
- 英语学习重要性
- URL、URI和URN三者之间的区别
- HDU 5522 Numbers(暴力)——BestCoder Round #61(div.2)
- Oracle实战——数据查询与更新
- CodeForces 589H Tourist Guide(树形DP)
- LightOJ - 1060 nth Permutation(计数)
- tyvj1044数字三角形
- Servlet 生命周期、工作原理
- tyvj1079数字三角形3
- Systems
- MOOC PTA 08-图8 How Long Does It Take
- BestCoder Round #61 (div.2) 1001
- SSH框架搭建图文详解
- 机器学习之回归分析——Logistic regression