Partitioning by Palindromes UVA - 11584(dp)最小回文划分(我爱喝水,天天健康)

Partitioning by Palindromes UVA - 11584

题意:

将一个串划分为最少的回文串。

思路:

  1. dp【i】第i个位置的最小划分。
  2. 转移方程dp【i】=min(dp【j-1】+1, s [ j − > i ] s[j->i] s[j>i]是回文串)。
  3. 对于上面的判断回文,可以打表预处理。

AC

#include <iostream>
#include <bits/stdc++.h>
#define For(i,x,y) for(int i=(x); i<=(y); i++)
#define fori(i,x,y) for(int i=(x); i<(y); i++)
#define rep(i,y,x) for(int i=(y); i>=(x); i--)
#define mst(x,a) memset(x,a,sizeof(x))
#define pb push_back
#define sz(a) (int)a.size()
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int,int>pa;
typedef pair<ll,ll>pai;
const int maxn = 1100;
int hui[maxn][maxn], dp[maxn];
char s[maxn];
int main()
{
   // ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int tt; scanf("%d", &tt);
    while(tt--){
        scanf("%s", s+1);
        int n = strlen(s+1);
        For(i,1,n)For(j,1,n)hui[i][j]=0,dp[i]=i;
        For(i,1,n){
            int p, q;
            q = p = i;
            while( p>=1 && q<=n && s[p]==s[q])hui[p][q] = 1,p--, q++;
            p = i; q = i+1;
            while( p>=1 && q<=n && s[p]==s[q])hui[p][q] = 1,p--, q++;
        }
        For(i,1,n)For(j,1,i)if(hui[j][i])dp[i] = min(dp[i], dp[j-1] + 1);
        printf("%d\n", dp[n]);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值