Atcoder Yet Another Palindrome Partitioning(状压dp)

Atcoder Yet Another Palindrome Partitioning

思路:

一个字符串满足条件的情况是奇数字母个数小于等于1,也就是异或起来是1<<j(0<=j<=25)或者是0

记mark是异或起来的值

状态转移:

dp[mark]=dp[mark]+1;

dp[mark]=min(dp[mark^(1<<j)]+1,dp[mark]);(0<=j<=25)

注意dp[0]被转移后可能会变成1,但是由它转移的需要dp[0]=0,所以每次记得把dp[0]变为0

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long 
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a)) 

const int N=2e5+5;
const int INF=0x3f3f3f3f;
int dp[(1<<26)+5];
string s;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>s;
    mem(dp,INF);
    dp[0]=0;
    int mark=0;
    int ans=0;
    for(int i=0;i<s.size();i++)
    {
        mark^=1<<(s[i]-'a');
        dp[mark]=dp[mark]+1;
        for(int j=0;j<26;j++)
        {
            dp[mark]=min(dp[mark^(1<<j)]+1,dp[mark]);
        }        
        ans=dp[mark];
        dp[0]=0;
    }
    cout<<ans<<endl;
    return 0;
}

 

posted @ 2017-11-01 10:47  Wisdom+.+  阅读(253)  评论(0编辑  收藏  举报