首页 > 其他 > 详细

Atcoder Yet Another Palindrome Partitioning(状压dp)

时间:2017-11-01 10:54:42      阅读:166      评论:0      收藏:0      [点我收藏+]

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)

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!