此题不错。给你一些字字符,要求你用这些字符构成一个回文串,求字典序第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 ;
}

最新文章

  1. emmet使用 及 notepadd++ emmet的安装
  2. 3D场景优化
  3. shell管道和重定向
  4. MongoDB基础知识 01
  5. java基础之类与对象1
  6. vmvare虚拟机经验
  7. MySQL集群(三)mysql-proxy搭建负载均衡与读写分离
  8. 【Unity与23种设计模式】观察者模式(Observer)
  9. 【转】深入分析 Parquet 列式存储格式
  10. 2017-2018-2 20165237 实验三《 敏捷开发与XP实践》实验报告
  11. 安装CentOS 7(转)
  12. phpstudy + dvws
  13. 深入详解美团点评CAT跨语言服务监控(三)CAT客户端原理
  14. CYS-Sqlite数据导入工具
  15. spring学习之springMVC 返回类型选择 以及 SpringMVC中model,modelMap.request,session取值顺序
  16. Oracle体系结构之密码文件管理
  17. 1001.A+B Format (20)的感受
  18. 解决 maven 项目启动 提示 class not find
  19. Oracle SQL Developer 查询时间格式
  20. 2017多校第10场 HDU 6172 Array Challenge 猜公式,矩阵幂

热门文章

  1. 在Javascript中 声明时用&quot;var&quot;与不用&quot;var&quot;的区别,== 和 ===的区别
  2. 程序员,Python 这次彻底上位了!
  3. linux 开机报错,error grub_efi_find_mmap_size not find
  4. [转载]MySQL面试题
  5. FICO(费埃哲)评分系统有什么优缺点?在国内的发展怎么样?
  6. PHP 预定义变量
  7. MyBatis学习(一)————纯jdbc编程
  8. cobbler部署以及使用
  9. TeamWork#3,Week5,Introduction to the &quot;take-away&quot; Sale Selection Project
  10. Scrum Meeting 10.24