[题解]UVA11027 Palindromic Permutation
链接:http://vjudge.net/problem/viewProblem.action?id=19602
描述:给出一个字符串,求重新排列后第n个回文串,若没有则输出”XXX“。
思路:组合数问题。
首先考虑什么时候有回文串。很简单,数量为奇数的字母不超过1个。且这个字母只能是在字符串的中间。
然后我们会发现,回文串的字典序就是字串前半部分的字典序。问题就转化成求字典序第n的字符串。于是我们可以试着模拟一下这个过程。首先把字典序最小的字母放在第一个位置,然后计算出后面字母的排列数,即固定下第一个位置后有多少种字符串,设这个数是Num。如果Num>=n,则第一个位置就确定了,继续考虑第二个位置;如果Num<n,则第一个位置的字母不合适,n-=Num,更换成字典序第二的字母,重复上述计算。如果一直是Num<n,则无解。
PS.zyy的数学不好,说得有点啰嗦,请各位大神不吝指教~
下面是我的实现:
1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <cmath>
5 using namespace std;
6 #define MaxLen 30
7 int T,N;
8 int Cnt[30],Lit;
9 char str[MaxLen+5],Ans[MaxLen+5];
10 bool flag;
11 inline int fac(int t)
12 {
13 if(t<=2)
14 return t;
15 int i,Ret=1;
16 for(i=2;i<=t;i++)
17 Ret*=i;
18 return Ret;
19 }
20 inline int Arg()
21 {
22 int i,Sum=0;
23 for(i=1;i<=26;i++)
24 {
25 if(!Cnt[i])
26 continue;
27 Sum+=Cnt[i];
28 }
29 Sum=fac(Sum);
30 for(i=1;i<=26;i++)
31 {
32 if(!Cnt[i])
33 continue;
34 Sum/=fac(Cnt[i]);
35 }
36 return Sum;
37 }
38 void Work(int pos,int n)
39 {
40 int i,Num;
41 for(i=1;i<=26;)
42 {
43 if(!Cnt[i])
44 {
45 i++; continue;
46 }
47 Cnt[i]--;
48 Num=Arg();
49 if(Num==0)
50 {
51 Ans[pos]='a'+i-1;
52 return;
53 }
54 else if(Num<n)
55 {
56 Cnt[i]++;
57 i++;
58 n-=Num;
59 }
60 else
61 {
62 Ans[pos]='a'+i-1;
63 Work(pos+1,n);
64 return;
65 }
66 }
67 flag=false;
68 }
69 int main()
70 {
71 int Len,i,JS;
72 scanf("%d",&T);
73 for(int t=1;t<=T;t++)
74 {
75 printf("Case %d: ",t);
76 scanf("%s",str); scanf("%d",&N);
77 Len=strlen(str); Lit=(Len+1)/2;
78 if(Len==1)
79 {
80 if(N==1)
81 printf("%s\n",str);
82 else
83 printf("XXX\n");
84 continue;
85 }
86 memset(Cnt,0,sizeof(Cnt));
87 for(i=0;i<Len;i++)
88 Cnt[str[i]-'a'+1]++;
89 JS=0;
90 for(i=1;i<=26;i++)
91 {
92 if(Cnt[i]%2)
93 {
94 JS++;
95 Ans[Lit]='a'+i-1;
96 }
97 Cnt[i]/=2;
98 }
99 if(JS>=2)
100 {
101 printf("XXX\n");
102 continue;
103 }
104 flag=true;
105 Work(1,N);
106 if(!flag)
107 printf("XXX\n");
108 else
109 {
110 for(i=1;i<=Lit;i++)
111 printf("%c",Ans[i]);
112 if(Lit*2==Len)
113 for(i=Lit;i>=1;i--)
114 printf("%c",Ans[i]);
115 else
116 for(i=Lit-1;i>=1;i--)
117 printf("%c",Ans[i]);
118 printf("\n");
119 }
120 }
121 return 0;
122 }
[题解]UVA11027 Palindromic Permutation的更多相关文章
- 【题解】CF359B Permutation
[题解]CF359B Permutation 求一个长度为\(2n\)的序列,满足\(\Sigma |a_{2i}-a_{2i-1}|-|\Sigma a_{2i}-a_{2i-1}|=2k\) 这种 ...
- LeetCode(3)题解: Longest Palindromic Substring
https://leetcode.com/problems/longest-palindromic-substring/ 题目: Given a string S, find the longest ...
- LeetCode题解——Longest Palindromic Substring
题目: 给定一个字符串S,返回S中最长的回文子串.S最长为1000,且最长回文子串是唯一. 解法: ①遍历,对于每个字符,计算以它为中心的回文子串长度(长度为奇数),同时计算以它和右边相邻字符为中心的 ...
- UVA 11027 - Palindromic Permutation
题目意思为解码字符串,要输出第n个回文字符串,因为对称关系,前一半确定了,后一半也就跟着确定了,所以n其实就是前一半字符串的编码,还要减去1,直接解码出来再复制给后半即可 #include<cs ...
- LeetCode题解之Palindromic Substrings
1.问题描述 2.问题分析 对于每一个字符,以该字符为中心计算回文个数. 3.代码 int countSubstrings(string s) { ; ) ; ; i < s.size(); i ...
- PAT甲题题解-1024. Palindromic Number (25)-大数运算
大数据加法给一个数num和最大迭代数k每次num=num+num的倒序,判断此时的num是否是回文数字,是则输出此时的数字和迭代次数如果k次结束还没找到回文数字,输出此时的数字和k 如果num一开始是 ...
- leetcode个人题解——#31 Next Permutation
写这题时脑子比较混乱,重写了一遍wiki大佬的解法. 算法: According to Wikipedia, a man named Narayana Pandita presented the fo ...
- LeetCode Palindrome Permutation II
原题链接在这里:https://leetcode.com/problems/palindrome-permutation-ii/ 题目: Given a string s, return all th ...
- 267. Palindrome Permutation II
题目: Given a string s, return all the palindromic permutations (without duplicates) of it. Return an ...
随机推荐
- Nginx 反向代理解决跨域问题分析
当你遇到跨域问题,不要立刻就选择复制去尝试.请详细看完这篇文章再处理 .我相信它能帮到你. 分析前准备: 前端网站地址:http://localhost:8080 服务端网址:http://local ...
- Solon Web 开发,四、请求上下文
Solon Web 开发 一.开始 二.开发知识准备 三.打包与运行 四.请求上下文 五.数据访问.事务与缓存应用 六.过滤器.处理.拦截器 七.视图模板与Mvc注解 八.校验.及定制与扩展 九.跨域 ...
- Spring Boot Starter 和 ABP Module
Spring Boot 和 ABP 都是模块化的系统,分别是Java 和.NET 可以对比的框架.模块系统是就像乐高玩具一样,一块一块零散积木堆积起一个精彩的世界.每种积木的形状各不相同,功能各不相同 ...
- 不难懂——th: 的常用标签
关键字> 功能介绍 > 案例 th:id 替换id <input th:id="'xxx' + ${collect.id}"/> ...
- asp.net core监控—引入Prometheus(三)
上一篇博文中说到Prometheus有四种指标类型:Counter(计数器).Gauge(仪表盘).Histogram(直方图).Summary(摘要),并且我们做了一个Counter的Demo,接下 ...
- 字节Android Native Crash治理之Memory Corruption工具原理与实践
作者:字节跳动终端技术--庞翔宇 内容摘要 MemCorruption工具是字节跳动AppHealth (Client Infrastructure - AppHealth) 团队开发的一款用于定 ...
- Task+ConcurrentQueue多线程编程
队列(Queue)代表了一个先进先出的对象集合.当您需要对各项进行先进先出的访问时,则使用队列.当您在列表中添加一项,称为入队,当您从列表中移除一项时,称为出队. ConcurrentQueue< ...
- NumPy 数组学习手册·翻译完成
原文:Learning NumPy Array 协议:CC BY-NC-SA 4.0 欢迎任何人参与和完善:一个人可以走的很快,但是一群人却可以走的更远. 在线阅读 ApacheCN 面试求职交流群 ...
- iptables简单使用
1.安装iptables yum install iptables-services 2.iptables简单使用 iptables防火墙文件路径/etc/sysconfig/iptables sys ...
- Maven警告解决:Using platform encoding (UTF-8 actually)
感谢原文作者:Scorpip_cc 原文链接:https://www.jianshu.com/p/9c8c01f6bebc 执行Maven Install打包的时候,提示以下警告信息: [WARNIN ...