UVA11027_Palindromic Permutation
2024-09-21 09:16:57
此题不错。给你一些字字符,要求你用这些字符构成一个回文串,求字典序第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 ;
}
最新文章
- emmet使用 及 notepadd++ emmet的安装
- 3D场景优化
- shell管道和重定向
- MongoDB基础知识 01
- java基础之类与对象1
- vmvare虚拟机经验
- MySQL集群(三)mysql-proxy搭建负载均衡与读写分离
- 【Unity与23种设计模式】观察者模式(Observer)
- 【转】深入分析 Parquet 列式存储格式
- 2017-2018-2 20165237 实验三《 敏捷开发与XP实践》实验报告
- 安装CentOS 7(转)
- phpstudy + dvws
- 深入详解美团点评CAT跨语言服务监控(三)CAT客户端原理
- CYS-Sqlite数据导入工具
- spring学习之springMVC 返回类型选择 以及 SpringMVC中model,modelMap.request,session取值顺序
- Oracle体系结构之密码文件管理
- 1001.A+B Format (20)的感受
- 解决 maven 项目启动 提示 class not find
- Oracle SQL Developer 查询时间格式
- 2017多校第10场 HDU 6172 Array Challenge 猜公式,矩阵幂
热门文章
- 在Javascript中 声明时用";var";与不用";var";的区别,== 和 ===的区别
- 程序员,Python 这次彻底上位了!
- linux 开机报错,error grub_efi_find_mmap_size not find
- [转载]MySQL面试题
- FICO(费埃哲)评分系统有什么优缺点?在国内的发展怎么样?
- PHP 预定义变量
- MyBatis学习(一)————纯jdbc编程
- cobbler部署以及使用
- TeamWork#3,Week5,Introduction to the ";take-away"; Sale Selection Project
- Scrum Meeting 10.24