此题不错。给你一些字字符,要求你用这些字符构成一个回文串,求字典序第k大的回文串。

首先通过给定的字符,我们可以直接判断能否构成回文串(奇数的字符不超过一种),其次可以统计每个字符在回文串的左边应该出现多少次。

然后从左到右判断每一位应该放那个字母,一边放置一遍更新即可。

我仅判断奇数次的个数为奇偶就ac了,输出的时候只输出了一个,如果有三个呢? 哈哈, 数据太水了。

召唤代码君:

#include <iostream>
#include <cstdio>
#include <cstring>
typedef long long ll;
using namespace std; int t[],c[],n,T,cas=,tot,pos;
char s[],ans[];
ll P[];
bool flag; ll count()
{
ll sum=,cur=;
for (int i=; i<; i++)
sum+=t[i],cur*=P[t[i]];
return P[sum]/cur;
} int main()
{
P[]=;
for (int i=; i<; i++) P[i]=P[i-]*i;
scanf("%d",&T);
while (T--)
{
flag=true;
scanf("%s%d",s,&n);
memset(t,,sizeof t);
pos=-;
tot=;
for (int i=; s[i]; i++) t[s[i]-'a']++;
for (int i=; i<; t[i]/=,tot+=t[i],i++)
if (t[i]&)
{
if (pos==-) pos=i;
else flag=false;
}
printf("Case %d: ",++cas);
if (!flag || count()<n)
{
puts("XXX");
continue;
}
for (int i=; i<=tot; i++)
{ for (int j=; j<; j++)
{
if (t[j]<=) continue;
t[j]--;
ll tmp=count();
if (n<=tmp)
{
c[i]=j+'a';
break;
}
t[j]++,n-=tmp;
}
}
for (int i=; i<=tot; i++) printf("%c",c[i]);
if (pos!=-) printf("%c",(char)('a'+pos));
for (int i=tot; i>=; i--) printf("%c",c[i]);
printf("\n");
}
return ;
}

UVA11027_Palindromic Permutation的更多相关文章

  1. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  2. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  3. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  4. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  5. [LeetCode] Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  6. Leetcode 60. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  7. UVA11525 Permutation[康托展开 树状数组求第k小值]

    UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...

  8. Permutation test: p, CI, CI of P 置换检验相关统计量的计算

    For research purpose, I've read a lot materials on permutation test issue. Here is a summary. Should ...

  9. Permutation

    (M) Permutations (M) Permutations II (M) Permutation Sequence (M) Palindrome Permutation II

随机推荐

  1. $(document)和$(window)各是什么意思?

    jquery中的对象$(document) 是当前文档,就是你看到的整个网页$(window) 如果没有框架则就是你浏览的当前浏览器的窗口 将document, window转换为jquery对象 比 ...

  2. rabbitMQ的三种路由模式

    rabbitMQ工作流程: 1.声明交换机 2.声明消息队列 3.绑定交换机和队列 4.生产者往交换机里发送新消息 5.交换机根据所选的模式和routingKey决定消息发往哪条消息队列 6.一个消费 ...

  3. Rmdir方法

    删除现有目录或文件夹. 语法 RmDir路径 所需的_路径_参数标识的目录或文件夹要移除的字符串表达式. _路径_可以包含驱动器. 如果没有指定驱动器, RmDir 将删除当前驱动器上的目录或文件夹. ...

  4. [PLC]ST语言二:LDP_LDF_ANDP_ANDF_ORP_ORF

    一:LDP_LDF_ANDP_ANDF_ORP_ORF基本指令 说明:简单的顺控指令不做其他说明. 控制要求:无 编程梯形图: 结构化编程ST语言: (*LDP(EN,s)/ORP(EN,S)*) M ...

  5. Cinema 4D* 中令人惊叹的体积效果

    原文链接 第一部分 第二部分 云.雾.星云.火状结构等体积效果是 3D 美术师日常工作中的常见任务.借助 Turbulence FD* 或 X-Particles* 等当代第三方插件进行流体或粒子模拟 ...

  6. C#多线程的几种使用

    参见链接   :http://www.jb51.net/article/46234.htm

  7. [笔试]CVTE 2019提前批 Windows应用开发笔试

    不定项选择(x20) 数据结构 以abcdefg的顺序入栈,不可能出现的出栈顺序 一棵二叉树给出中序遍历和后序遍历结果,求左子树的节点数 操作系统 Linux中用什么指令可以找到文件中所有以" ...

  8. JVM类加载全过程--图解

    JVM规范允许类加载器在预料某个类将要被使用时就预先加载它,下图为实例方法被调用时的JVM内存模型,1~7完整的描述了从类加载开始到方法执行前的预备过程,后面将对每一个步骤进行解释 在我们加载类的过程 ...

  9. openstack horizon开发第一天

    horizon插件构造 创建一个dashboardmkdir opesntack_dashboard/dashboards/mydashboardpython manage.py startdash ...

  10. Docker 私有仓库方案比较与搭建

    我们知道docker镜像可以托管到dockerhub中,跟代码库托管到github是一个道理.但如果我们不想把docker镜像公开放到dockerhub中,只想在部门或团队内部共享docker镜像,能 ...