题目链接:http://codeforces.com/problemset/problem/883/H

Time limit: 3000 ms

Memory limit: 262144 kB

Kolya has a string s of length n consisting of lowercase and uppercase Latin letters and digits.

He wants to rearrange the symbols in s and cut it into the minimum number of parts so that each part is a palindrome and all parts have the same lengths. A palindrome is a string which reads the same backward as forward, such as madam or racecar.

Your task is to help Kolya and determine the minimum number of palindromes of equal lengths to cut s into, if it is allowed to rearrange letters in s before cuttings.

Input

The first line contains an integer n (1 ≤ n ≤ 4·105) — the length of string s.

The second line contains a string s of length n consisting of lowercase and uppercase Latin letters and digits.

Output

Print to the first line an integer k — minimum number of palindromes into which you can cut a given string.

Print to the second line k strings — the palindromes themselves. Separate them by a space. You are allowed to print palindromes in arbitrary order. All of them should have the same length.

Example

Input
6
aabaac
Output
2
aba aca
Input
8
0rTrT022
Output
1
02TrrT20
Input
2
aA
Output
2
a A

题意:

给出一个长度为n的字符串,可以任意的重新排列,求最小能将该字符串切割成等长的多少份,使得每一份均为回文串。

题解:

(引用来自http://blog.csdn.net/mMingfunnyTree/article/details/78306596的思路。)

不难想到,记录下每个字符出现的次数,要么是出现奇数次,要么是出现偶数次,记为cnt[i]。

假设长度为n的字符串,切割成m份,每份长为len(即 m * len = n),那么显然,len要么是奇数,要么是偶数;

若len为偶数,根据回文串的性质,就会推出 所有字符出现的次数都为偶数;

换句话说,一旦cnt[ '0'~'9' & 'a'~'z' & 'A'~'Z' ]中任意一个cnt[i]为奇数,len就必须为奇数;

那么我们先把情况分成两种:

  所有cnt[i]均为偶数,这时len可以为偶数,而要m最小,显然此时m可以为1,len==n;

  把字符串调整为回文串输出即可。

  存在一些cnt[i]为奇数,这时len必须为奇数,根据奇数长度的回文串的性质可知:

  每个回文串,中间位置单独一个字符;剩余均为成对出现的字符;

  例如"aabbbaa":单独一个字符'b';成对出现字符'a','a','b';

  这样我们就不难想到一种思路:

    遍历 '0'~'9' & 'a'~'z' & 'A'~'Z',根据cnt[i]的奇偶性,分成两组:odd和even;

    odd集合中每个元素代表一个成单的字符;even集合中每个元素代表一对成双的字符;

    这样一来,目标情况是:

    odd集合的size正好为m,这样odd内的每个元素正好作为一个回文小串的中间位,

    而even集合中的元素应当正好可以平均分配到m个回文小串中( 即满足m | even.size()  <=>  odd.size() | even.size() );

    那如果even.size()不能平均分成m份怎么办?

    此时,显然只能把even中的一个元素,即一对字符,拆开来,当做两个成单字符;即even.size() -= 1 , m += 2;

    反复如此,直到even中的元素可以平均分配成m份为止(even.size()==0时必然可以平均分配,此时m = odd.size() = n );

AC代码:

#include<bits/stdc++.h>
#define MAX 400005
using namespace std;
int n;
char str[MAX];
int cnt[]={};
vector<char> odd,even;
int main()
{
scanf("%d",&n);
scanf("%s",&str);
for(int i=;i<n;i++) cnt[str[i]]++;
for(int i='';i<='z';i++)
{
if(cnt[i]<=) continue;
if(cnt[i]%)
{
odd.push_back((char)i);
cnt[i]--;
}
while(cnt[i])
{
even.push_back(i);
cnt[i]-=;
}
} char ans[MAX];
if(odd.empty())
{
printf("1\n");
for(int i=;i<n/;i++) ans[i]=ans[n--i]=even[i];
ans[n]='\0';
printf("%s\n",ans);
return ;
} while(even.size() % odd.size())
{
odd.push_back(even.back());
odd.push_back(even.back());
even.pop_back();
} printf("%d\n",odd.size());
int len=n/odd.size();
while(!odd.empty())
{
ans[len/]=odd.back();
odd.pop_back();
for(int j=;j<len/;j++)
{
ans[j]=ans[len--j]=even.back();
even.pop_back();
}
ans[len]='\0';
printf("%s ",ans);
}
}

codeforces 883H - Palindromic Cut - [字符串处理]的更多相关文章

  1. 883H - Palindromic Cut(思维+STL)

    题目链接:http://codeforces.com/problemset/problem/883/H 题目大意:给一段长度为n的字符串s,想让你把s切成几段长度相同的回文串,可以改变s中字符的排列, ...

  2. CodeForces 835D - Palindromic characteristics | Codeforces Round #427 (Div. 2)

    证明在Tutorial的评论版里 /* CodeForces 835D - Palindromic characteristics [ 分析,DP ] | Codeforces Round #427 ...

  3. cut字符串截取

    cut字符串截取 -d 按字节截取 [root@slave elk]# ll total 0 drwxr-xr-x. 6 root root 194 Jan 24 16:15 bigdesk 截取前2 ...

  4. CodeForces 176B Word Cut dp

    Word Cut 题目连接: http://codeforces.com/problemset/problem/176/C Description Let's consider one interes ...

  5. Codeforces 176B (线性DP+字符串)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...

  6. CodeForces 176B Word Cut (计数DP)

    Word Cut Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit St ...

  7. Longest Palindromic Substring - 字符串中最长的回文字段

    需求:Given a string S, find the longest palindromic substring in S. You may assume that the maximum le ...

  8. Codeforces 316G3 Good Substrings 字符串 SAM

    原文链接http://www.cnblogs.com/zhouzhendong/p/9010851.html 题目传送门 - Codeforces 316G3 题意 给定一个母串$s$,问母串$s$有 ...

  9. Codeforces 1090B - LaTeX Expert - [字符串模拟][2018-2019 Russia Open High School Programming Contest Problem B]

    题目链接:https://codeforces.com/contest/1090/problem/B Examplesstandard input The most famous characters ...

随机推荐

  1. Java多线程之可见性与原子性——synchronized VS volatile

    <转:http://blog.csdn.net/uniquewonderq/article/details/48113071> 程序举例: 代码: package com.synch; p ...

  2. SpringBoot------添加保存时自动编译插件

    .右键Java项目 .选择“Spring Tools” 3.选择“Add Boot DevTools” 4.每次使用Ctrl + S键时就会自动编译了 实际上是在Pom.xml文件中添加了如下Java ...

  3. CentOS 6.4 命令行 安装 VMware Tools

    新建cdrom挂载目录 mkdir /mnt/cdrom 挂载光驱 mount -t auto /dev/cdrom /mnt/cdrom这命令就是把CentOS CDROM挂载在/mnt/cdrom ...

  4. 使用InternetGetConnectedState判断本地网络状态(C#举例)

    函数原型:函数InternetGetConnectedState返回本地系统的网络连接状态. 语法: BOOL InternetGetConnectedState( __out LPDWORD lpd ...

  5. error C4996: Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct

    使用VS13 跟  google protocbuf时出现了这个问题:真蛋疼,用别人的东西你就说不安全,用你自己的东西时你怎么不说不安全来着! 解决方案 在protoc   生成的头文件中加上 #pr ...

  6. 【代码审计】711cms_V1.0.5 目录遍历漏洞分析

      0x00 环境准备 711CMS官网: https://www.711cms.com/ 网站源码版本:711CMS 1.0.5 正式版(发布时间:2018-01-20) 程序源码下载:https: ...

  7. Jsoup(一)-- HelloWorld

    1.简介 jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQuery的操作方法来取出和操作数据. ...

  8. HttpClient(四)-- 使用代理IP 和 超时设置

    1.代理IP的用处: 在爬取网页的时候,有的目标站点有反爬虫机制,对于频繁访问站点以及规则性访问站点的行为,会采集屏蔽IP措施.这时候,就可以使用代理IP,屏蔽一个就换一个IP. 2.代理IP分类: ...

  9. Linux命令之乐--sed

    sed是stream edit的缩写,是处理文本非常重要的工具. 常见用法: 1. 替换文本 1.1 替换文本中的第一处符合的样式 sed 's/pattern/replace_string' fil ...

  10. Python系统编程笔记

    01. 进程与程序 编写完毕的代码,在没有运行的时候,称之为程序 正在运行着的代码,就称为进程 进程是系统分配资源的最小单位. 进程资源包括: 中间变量 代码 计数器 02. 通过os.fork()函 ...