atcoder CODE FESTIVAL 2017 qual C D - Yet Another Palindrome Partitioning
Problem Statement
We have a string s consisting of lowercase English letters. Snuke is partitioning s into some number of non-empty substrings. Let the subtrings obtained be s1, s2, …, sNfrom left to right. (Here, s=s1+s2+…+sN holds.) Snuke wants to satisfy the following condition:
- For each i
Find the minimum possible value of N when the partition satisfies the condition.
用二進制記下前綴的每種字母奇偶性
dp[i]表示前i個最少分幾段,枚舉奇數字母是什麽轉移
可以記下每種二進制最小的dp值是什麽
#include<bits/stdc++.h> using namespace std;View Codechar s[200005]; int f[1<<26]; int main(){ scanf("%s",&s); int n=strlen(s),td; for (int i=1;i<(1<<26);i++) f[i]=1000000000; for (int i=0,zt=0;i<n;i++){ zt^=(1<<(s[i]-‘a‘)); td=f[zt]+1; for (int j=0;j<26;j++) td=min(td,f[zt^(1<<j)]+1); f[zt]=min(f[zt],td); } printf("%d",td); }
Time limit : 3sec / Memory limit : 512MB
Score : 700 points
Problem Statement
We have a string s consisting of lowercase English letters. Snuke is partitioning s into some number of non-empty substrings. Let the subtrings obtained be s1, s2, …, sNfrom left to right. (Here, s=s1+s2+…+sN holds.) Snuke wants to satisfy the following condition:
- For each i (1≤i≤N), it is possible to permute the characters in si and obtain a palindrome.
Find the minimum possible value of N when the partition satisfies the condition.
Constraints
- 1≤|s|≤2×105
- s consists of lowercase English letters.
Input
Input is given from Standard Input in the following format:
s
Output
Print the minimum possible value of N when the partition satisfies the condition.
Sample Input 1
Copyaabxyyzz
Sample Output 1
Copy2
The solution is to partition s as aabxyyzz
= aab
+ xyyzz
. Here, aab
can be permuted to form a palindrome aba
, and xyyzz
can be permuted to form a palindrome zyxyz
.
Sample Input 2
Copybyebye
Sample Output 2
Copy1
byebye
can be permuted to form a palindrome byeeyb
.
Sample Input 3
Copyabcdefghijklmnopqrstuvwxyz
Sample Output 3
Copy26
Sample Input 4
Copyabcabcxabcx
Sample Output 4
Copy3
The solution is to partition s as abcabcxabcx
= a
+ b
+ cabcxabcx
.
atcoder CODE FESTIVAL 2017 qual C D - Yet Another Palindrome Partitioning