码迷,mamicode.com
首页 > 其他好文 > 详细

Atcoder Yet Another Palindrome Partitioning(状压dp)

时间:2017-11-01 11:06:24      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:syn   sync   异或   sizeof   ace   name   style   pre   turn   

Atcoder Yet Another Palindrome Partitioning

思路:

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

记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;
}

 

Atcoder Yet Another Palindrome Partitioning(状压dp)

标签:syn   sync   异或   sizeof   ace   name   style   pre   turn   

原文地址:http://www.cnblogs.com/widsom/p/7765362.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!