1044 - Palindrome Partitioning(区间DP)

题目大意:

给你一个字符串,问这个字符串最少有多少个回文串。

区间DP直接搞

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;
const int INF = 1e9+7;
const int MAXN = 1055;
int dp[MAXN][MAXN];
bool P[MAXN][MAXN];
char str[MAXN];
int DFS(int L,int R)
{
    if(dp[L][R] != -1)
        return dp[L][R];
    if(L >= R)
        return dp[L][R] = 1;

    dp[L][R] = INF;
    if(P[L][R]) dp[L][R] = 1;
    for(int i=L; i<=R; i++)
    {
        if(P[L][i])
            dp[L][R] = min(dp[L][R], 1 + DFS(i+1,R));
    }
    return dp[L][R];
}

bool ok(int L,int R)
{
    for(int i=L,j=R; i<=j; i++, j--)
    {
        if(str[i] != str[j])
            return false;
    }
    return true;
}

int main()
{
    int T, cas = 1;
    scanf("%d", &T);
    while(T --)
    {
        memset(dp, -1, sizeof(dp));
        scanf("%s", str);
        int len = strlen(str) - 1;
        for(int i=0; i<=len; i++)
        for(int j=i; j<=len; j++)
            P[i][j] = ok(i, j);

        printf("Case %d: %d\n",cas ++, DFS(0, len) );
    }

    return 0;
}
时间: 2024-10-16 00:29:24

1044 - Palindrome Partitioning(区间DP)的相关文章

lightoj-1044 - Palindrome Partitioning(区间dp)

1044 - Palindrome Partitioning PDF (English) Statistics ForumTime Limit: 1 second(s) Memory Limit: 32 MBA palindrome partition is the partitioning of a string such that each separate substring is a palindrome. For example, the string "ABACABA" c

Lightoj 1044 - Palindrome Partitioning (DP)

题目链接: Lightoj  1044 - Palindrome Partitioning 题目描述: 给一个字符串,问至少分割多少次?分割出来的子串都是回文串. 解题思路: 先把给定串的所有子串是不是回文串处理出来,然后用dp[i] 表示 从起点到串i的位置的最少分割次数,然后结合处理出来的回文串转移一下即可! 还是好蠢哦!自己竟然感觉是一个区间DP,但是n又那么大,完全不是区间DP的作风啊! 1 #include <cmath> 2 #include <cstdio> 3 #i

HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)

题目 参考自博客:http://blog.csdn.net/u011498819/article/details/38356675 题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. 简单的区间dp,哎,以为很神奇的东西,其实也是dp,只是参数改为区间,没做过此类型的题,想不到用dp,以后就 知道了,若已经知道[0,i],推[0,i+1], 显然还要从i+1 处往回找,dp方程也简单: dp[j][i]=(dp[j+1][i]+dp[j][i-1]+10007-dp[j+1][

POJ 3280 Cheapest Palindrome(区间DP求改成回文串的最小花费)

题目链接:http://poj.org/problem?id=3280 题目大意:给你一个字符串,你可以删除或者增加任意字符,对应有相应的花费,让你通过这些操作使得字符串变为回文串,求最小花费.解题思路:比较简单的区间DP,令dp[i][j]表示使[i,j]回文的最小花费.则得到状态转移方程: dp[i][j]=min(dp[i][j],min(add[str[i]-'a'],del[str[i]-'a'])+dp[i+1][j]); dp[i][j]=min(dp[i][j],min(add[

Light oj 1044 - Palindrome Partitioning(区间dp)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1044 dp[i][j]表示i到j直接的最小回文区间个数,直接看代码 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int N = 1e3 + 5; 4 int dp[N][N], inf = 1e9; 5 char str[N]; 6 bool judge(int l, int r) { 7 for(int i

LightOJ 1044 Palindrome Partitioning(简单字符串DP)

A palindrome partition is the partitioning of a string such that each separate substring is a palindrome. For example, the string "ABACABA" could be partitioned in several different ways, such as {"A","B","A","

HDU 4632 Palindrome subsequence (区间dp 容斥定理)

Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/Others) Total Submission(s): 2610    Accepted Submission(s): 1050 Problem Description In mathematics, a subsequence is a sequence that can be derived

uva 10453 Make Palindrome (区间DP + 递归输出)

uva 10453 Make Palindrome 题目大意:给出一段字符串,要求求出最少加入几个字符(任意位置),可以让该字符串变成会问字符串,并输出修改以后的回文字符串. 解题思路:dp[i][j]代表了将该字符串从第i位到第j位变成回文字符串最少要添加的字符.当S[i]==S[j],dp[i][j]=dp[i+1][j?1]当S[i]!=S[j],dp[i][j]=min(dp[i+1][j],dp[i][j?1])+1,在DP的过程中记录对该区间的操作类型,最后递归输出. #includ

POJ 3280 Cheapest Palindrome(区间DP)

 Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6894   Accepted: 3344 Description Keeping track of all the cows can be a tricky task so Farmer John has installed a system to automate it. He has installed on each co