codeforc H. Palindromic Cut
来源:互联网 发布:数据表格平台 编辑:程序博客网 时间:2024/06/03 16:50
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.
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.
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.
6aabaac
2aba aca
80rTrT022
102TrrT20
2aA
2a 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;}
- codeforc H. Palindromic Cut
- codeforces 883H Palindromic Cut
- codefoces 883H Palindromic Cut
- NEERC 17 H. Palindromic Cut 暴力(回文)
- CF 883H Palindromic Cut(模拟)
- 2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest H. Palindromic Cut
- cut
- cut
- cut
- cut
- cut
- cut
- cut
- cut
- Cut
- cut
- cut
- cut
- 完全卸载oracle
- 二分搜索(1)
- SIGCLD语义
- 麻将算法(七)胡牌之对子判断
- 【mongodb】3.X版本的 的用户认证
- codeforc H. Palindromic Cut
- Java实现全屏截取并保存成图片
- Java_关于数组的小结
- flask view中装饰器失效问题
- android 实现apk下载展现通知栏
- SQL SEVER基础
- 希腊字母含义及快捷键输入
- 斯坦福机器学习第六周(模型选择、高偏差、高方差、学习曲线)
- HashMap和HashTable区别