状态压缩Dp

本文探讨了如何通过动态规划解决字符串子序列回文问题,包括输入格式、算法实现及输出解析,适用于初学者理解字符串操作及动态规划的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Pieces

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1355    Accepted Submission(s): 688


Problem Description
You heart broke into pieces.My string broke into pieces.But you will recover one day,and my string will never go back.
Given a string s.We can erase a subsequence of it if this subsequence is palindrome in one step.  We should take as few steps as possible to erase the whole sequence.How many steps do we need?
For example, we can erase abcba from axbyczbea and get xyze in one step.
 

Input
The first line contains integer T,denote the number of the test cases. Then T lines follows,each line contains the string s (1<= length of s <= 16).
T<=10.
 

Output
For each test cases,print the answer in a line.
 

Sample Input
  
2 aa abb
 

Sample Output
  
1 2
 

Source
 

Recommend
zhuyuanchen520

/*
 * Author:  ****
 * Created Time:  2013/9/18 16:11:27
 * File Name: A.cpp
 * solve: A.cpp
 */
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<stack>
#include<set>
#include<iostream>
#include<vector>
#include<queue>
//ios_base::sync_with_stdio(false);
//#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;
#define sz(v) ((int)(v).size())
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define repf(i, a, b) for (int i = (a); i <= (b); ++i)
#define repd(i, a, b) for (int i = (a); i >= (b); --i)
#define clr(x) memset(x,0,sizeof(x))
#define clrs( x , y ) memset(x,y,sizeof(x))
#define out(x) printf(#x" %d\n", x)
#define sqr(x) ((x) * (x))
typedef long long LL;

const int INF = 1000000000;
const double eps = 1e-8;
const int maxn = (1<<16);

int sgn(const double &x) {  return (x > eps) - (x < -eps); }

char str[20];
bool is_palindrome[maxn];
int dp[maxn];
int len;
void init()
{
    char buf[20];
    rep(i,0,1<<len)
    {
        int cnt = 0;
        rep(j,0,len)
        {
            if((1<<j) & i)
            {
                buf[cnt] = str[j];
                cnt++;
            }
        }
        int flag = 1;
        rep(j,0,cnt)
        {
            if(buf[j] != buf[cnt - 1 - j])
            {
                flag = 0;
                break;
            }
        }
        is_palindrome[i] = flag;
    }
    return ;
}

void d()
{
    dp[0] = 0;
    rep(i,1,1<<len)
    {
        dp[i] = INF;
        for(int sub = i;sub > 0;sub = (sub - 1)&i)
        {
            if(is_palindrome[sub])
                dp[i] = min(dp[i],dp[sub^i] + 1);
        }
    }
}
int main() 
{
    //freopen("in.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",str);
        len = strlen(str);
        init();
        d();
        cout<<dp[(1<<len) - 1]<<endl;
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值