此题不错。给你一些字字符,要求你用这些字符构成一个回文串,求字典序第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. CSS3设置背景图片的大小

    设置背景图片的大小,以长度值或百分比显示,还可以通过cover和contain来对图片进行伸缩. background-size 语法详解: 要在插入图片之后进行设置背景图片的大小 backgroun ...

  2. asp.net core 自定义中间件和service

    首先新建项目看下main方法: public static void Main(string[] args) { var host = new WebHostBuilder() .UseKestrel ...

  3. Mysql试题集锦

    1.一张表,里面有 ID 自增主键,当 insert 了 17 条记录之后,删除了第 15,16,17 条记录,再把 Mysql 重启,再 insert 一条记录,这条记录的 ID 是 18 还是 1 ...

  4. 17-使用公共 Registry

    Docker Hub 是 Docker 公司维护的公共 Registry.用户可以将自己的镜像保存到 Docker Hub 免费的 repository 中.如果不希望别人访问自己的镜像,也可以购买私 ...

  5. 关于docker线上部署时间问题

    背景 公司线上部署采用docker swarm方式,这几天线上项目时间突然出了问题(ps:第一反应,我去,这也能出问题,代码里肯定藏毒了),线上时间总跟实际时间差八个小时.本着速战速决的原则,把所有时 ...

  6. js/jquery去掉空格,回车,换行示例代码

    Jquery: $("#accuracy").val($("#accuracy").val().replace(/\ +/g,""));// ...

  7. linux简单命令常用随记

    //查看网络信息 ifconfig //修改ip地址 ifconfig eth0 123.123.123.123 netmask 255.255.255.0 //网关设置 route add defa ...

  8. 三维空间中xoy平面上特定抛物线的正等测投影解析解的一种求法

    背景 背景:为锻炼代同学,老师给了她一个反向工程微信"跳一跳"小游戏的任务,希望做一个一样的出来.跳一跳中,有方块,有小人,小人站在方块上. 这个游戏的玩法是,用手指按住手机屏幕, ...

  9. 学霸网站-Alpha版本发布说明

    项目名称 学霸网站 项目版本 Alpha 项目团队 ourteam 发布日期 2014-11-23 一.版本的新功能 1.匿名提问 用户提问的时候可以选择匿名提问,这样在问题的详细信息不会显示提出者的 ...

  10. Chapter 5 软件工程中的形式化方法

    从广义上讲,形式化方法是指将离散数学的方法用于解决软件工程领域的问题,主要包括建立精确的数学模型以及对模型的分析活动.狭义的讲,形式化方法是运用形式化语言,进行形式化的规格描述.模型推理和验证的方法. ...