11584 - Partitioning by Palindromes——[DP]

Posted kiraa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了11584 - Partitioning by Palindromes——[DP]相关的知识,希望对你有一定的参考价值。

题意分析:

  题目比较容易理解,以d[i]表示前i个字符的最优解,状态转移方程 d[i]=min{d[j]+1| [j+1~i]为回文串}

代码如下:

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 const int maxs=1000;
 7 int is[maxs+5][maxs+5];
 8 int dp[maxs];
 9 char s[maxs];
10 void pre_solve(){
11     memset(is, 0, sizeof is);
12     int len=strlen(s);
13     for(int i=0;i<len;i++){
14         int l=i;
15         int r=i;
16         while(l>=0&&r<len){
17             if(s[l]==s[r])
18                 is[l][r]=1;
19             else break;
20             l--;
21             r++;
22         }
23     }
24     for(int i=0;i<len-1;i++){
25         int l=i;
26         int r=i+1;
27         while(l>=0&&r<len){
28             if(s[l]==s[r])
29                 is[l][r]=1;
30             else break;
31             l--;
32             r++;
33         }
34     }
35 }
36 int main(){
37     int T;
38     scanf("%d",&T);
39     while(T--){
40         scanf("%s",s);
41         pre_solve();
42         dp[0]=0;
43         int len=strlen(s);
44         for(int i=1;i<=len;i++){
45             dp[i]=1<<30;
46             for(int j=0;j<i;j++){
47                 if(is[j][i-1])
48                     dp[i]=min(dp[i],dp[j]+1);
49             }
50         }
51         printf("%d\n",dp[len]);
52     }
53 }

 

以上是关于11584 - Partitioning by Palindromes——[DP]的主要内容,如果未能解决你的问题,请参考以下文章

UVA - 11584 Partitioning by Palindromes[序列DP]

11584 - Partitioning by Palindromes——[DP]

题解UVA11584 Partitioning by Palindromes

uva 11584 Partitioning by Palindromes 线性dp

UVA 11584Partitioning by Palindromes

UVA11584 Partitioning by Palindromes