codeforc H. Palindromic Cut

来源:互联网 发布:数据表格平台 编辑:程序博客网 时间:2024/06/03 16:50
H. Palindromic Cut
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 havethe same lengths. A palindrome is a string which reads the same backward as forward, such asmadam or racecar.

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

Input

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

The second line contains a string s of lengthn 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.

Examples
Input
6aabaac
Output
2aba aca 
Input
80rTrT022
Output
102TrrT20 
Input
2aA
Output
2

a A

给你一个字符串,需要把它以最小的划分次数划分为x个长度相等的回文串,可以重新排列。

分别统计出现一次的字符和出现两次的字符,如果没有出现一次的字符,那么所有字符出现次数均为偶数,说明本身便可以排列成回文串。

如果某个字符出现次数为偶次,可以拆分为多个cnt/2个相同字符存入,如果出现次数为奇数次,则先存入单个统计并计数减1,再存入双个统计,

如果单个字符数量不足,则需要用双个字符填充。

#include<stdio.h>#include<algorithm>#include<string.h>#include<vector>using namespace std;const int maxm = 500005;char str[maxm], ans[maxm];int cnt[maxm];vector<char>v1;vector<char>v2;int main(){int n, i, j, k, sum;scanf("%d", &n);scanf("%s", str);for (i = 0;i < n;i++) cnt[str[i]]++;for (i = 1;i <= 256;i++){if (cnt[i]){if (cnt[i] % 2 == 1) v1.push_back(i), cnt[i]--;while (cnt[i]){v2.push_back(i);cnt[i] -= 2;}}}if (v1.size() == 0){for (i = 0;i < n / 2;i++){ans[i] = ans[n - i - 1] = v2.back();v2.pop_back();}ans[n] = '\0';printf("1\n%s\n", ans);return 0;}while (v2.size() % v1.size()){v1.push_back(v2.back());v1.push_back(v2.back());v2.pop_back();}printf("%d\n", v1.size());int len = n / v1.size();for (i = 0;i < v1.size();i++){ans[len / 2] = v1[i];for (j = 0;j < len / 2;j++){ans[j] = ans[len - j - 1] = v2.back();v2.pop_back();}ans[len] = '\0';printf("%s ", ans);}printf("\n");return 0;}