此题不错。给你一些字字符,要求你用这些字符构成一个回文串,求字典序第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. 总结一下公司项目使用各种较新的前端技术和 Api 的一些经验。

    关于 ES6: 需要注意 ES6 的一些特性和 API 是需要一个 200k 的 Polyfill 才能得到支持的,特性如 for ... of 循环,generator,API 如 Object.a ...

  2. Apache Flink学习笔记

    Apache Flink学习笔记 简介 大数据的计算引擎分为4代 第一代:Hadoop承载的MapReduce.它将计算分为两个阶段,分别为Map和Reduce.对于上层应用来说,就要想办法去拆分算法 ...

  3. Linux shell 编写(1)

    shell脚本的编写步骤 1.创建以.sh为扩展名的文件           touch 2.编辑脚本文件                              vim 3.增加脚本文件执行权限 ...

  4. 《数据结构与算法图解》 分享 pdf下载

    链接:https://pan.baidu.com/s/1gOMlwU5ucHYDVazvVMk2uw提取码:bk5x

  5. 快手hr面

    快手hr面 20180918 自我介绍 hr部门介绍 效率工程 主要问题 问我对部门是否有感兴趣? 我要求地点在北京,然后就畅聊口音.老家,学校等 学校的成绩?(研究生.本科) 自己属于哪类学生?(属 ...

  6. Python 利用 BeautifulSoup 爬取网站获取新闻流

    0. 引言 介绍下 Python 用 Beautiful Soup 周期性爬取 xxx 网站获取新闻流: 图 1 项目介绍 1. 开发环境 Python: 3.6.3 BeautifulSoup:   ...

  7. hadoop之定制自己的sort过程

    Key排序 1. 继承WritableComparator 在hadoop之Shuffle和Sort中,可以看到mapper的输出文件spill文件需要在内存中排序,并且在输入reducer之前,不同 ...

  8. 前端_CSS

    目录 CSS语法 CSS的四种引入方式 CSS选择器 CSS属性操作 补充 示例(一些小模板) CSS语法 CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明. 1 2 3 4 5 6 7 ...

  9. to_char

    to_date(to_char(to_date(#{conds.currentTime,jdbcType=VARCHAR},'YYYY-MM-DD hh24:mi:ss'),'hh24:mi:ss') ...

  10. idea打断点是灰色的

    点击这个图标,debug的断点就是灰色的,debug功能被禁用