UVa 11584 - Partitioning by Palindromes(线性DP + 预处理)

链接:

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2631

题意:

输入一个由小写字母组成的字符串(长度不超过1000),你的任务是把它划分成尽量少的回文串。
例如,racecar本身就是回文串;fastcar只能分成7个单字母的回文串,aaadbccb最少分成3个回文串:aaa, d, bccb。

分析:

设d[i]为字符0~i划分成的最小回文串的个数,则d[i] = min{d[j] + 1 | s[j+1~i]是回文串}。
可以先用O(n*n)时间预处理s[i..j]是否为回文串。方法是枚举中心,然后不断向左右延伸,直到左右字符不同为止。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5
 6 const int UP = 1000 + 5;
 7 int d[UP]; // d[i]为前i个字符划分成的最小回文串个数
 8 char s[UP];
 9 bool isp[UP][UP]; // isp[i][j]标记s[i..j]是否为回文串
10
11 void init(char* s, int len){
12     memset(isp, false, sizeof(isp));
13     for(int t = 1; t <= len; t++){
14         for(int f = t, b = t; 1 <= f && b <= len; f--, b++){
15             if(s[f] == s[b]) isp[f][b] = true;
16             else break;
17         }
18         for(int f = t, b = t + 1; 1 <= f && b <= len; f--, b++){
19             if(s[f] == s[b]) isp[f][b] = true;
20             else break;
21         }
22     }
23 }
24
25 int main(){
26     int T, len;
27     scanf("%d", &T);
28     while(T--){
29         scanf("%s", s + 1);
30         init(s, len = strlen(s + 1));
31         for(int t = 1; t <= len; t++){
32             d[t] = t;
33             for(int i = 1; i <= t; i++)
34                 if(isp[i][t]) d[t] = min(d[t], d[i-1] + 1);
35         }
36         printf("%d\n", d[len]);
37     }
38     return 0;
39 }

原文地址:https://www.cnblogs.com/hkxy125/p/8576295.html

时间: 2025-01-05 10:18:31

UVa 11584 - Partitioning by Palindromes(线性DP + 预处理)的相关文章

uva 11584 Partitioning by Palindromes 线性dp

// uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串的数目 // // f[i] = min(f[i],f[j-1] + 1(j到i是回文串)) // // 这道题还是挺简单的,继续练 #include <algorithm> #include <bitset> #include <cassert> #include <

UVA - 11584 Partitioning by Palindromes[序列DP]

UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters is a palindrome if it is the same written forwards and backwards. For example, ‘racecar’ is a palindrome, but ‘fastcar’ is not. A partition of a sequence of characters is a lis

uva 11584 Partitioning by Palindromes(dp)

题目链接 题意:给定一个字符串,分解成多个子串,每个子串都是回文串,问最少能分成多少个子串. 题解: dp[i]表示前i个字符串分割成最少回文子串的数量: 0<=j<=i;如果字符串从j到i是回文串,那么dp[i]=min(dp[i],dp[j-1]+1); #include <iostream> using namespace std; int dp[1005]; string s; bool ok(int j,int i) { while(j<=i) { if(s[j]!

UVa 11584 Partitioning by Palindromes【DP】

题意:给出一个字符串,问最少能够划分成多少个回文串 dp[i]表示以第i个字母结束最少能够划分成的回文串的个数 dp[i]=min(dp[i],dp[j]+1)(如果从第j个字母到第i个字母是回文串) 想不明白的还是初始化 初始化为:dp[i]=i+1, 后来= =,发现应该是这样的 从第1个字母到第i个字母最多能够划分成i+1个回文串, 所以为了求最小值,每一个初始值初始化为一个极大地值, 所以dp[i]初始化为INF也可以 1 #include<iostream> 2 #include&l

UVa 11584 Partitioning by Palindromes (简单DP)

题意:给定一个字符串,求出它最少可分成几个回文串. 析:dp[i] 表示前 i 个字符最少可分成几个回文串,dp[i] = min{ 1 + dp[j-1] | j-i是回文}. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath&g

UVa 11584 Partitioning by Palindromes

/*---UVa 11584 Partitioning by Palindromes --用dp[i]表示前i个字母划分成最小回文串的个数,则有dp[i]=min{dp[j]+1}s[j+1...i]是一个回文串,状态O(n)个 每次有O(n)个决策,而判断是否是回文串复杂度O(n),这样总的复杂度O(n^3).如果事先预处理标记一下s[i...j]是否构成 回文串,这样总的复杂度O(n^2).标记s[i...j]是否构成回文串,用vis[i][j](i<=j)来标记,if s[i]!=s[j]

区间DP UVA 11584 Partitioning by Palindromes

题目传送门 1 /* 2 题意:给一个字符串,划分成尽量少的回文串 3 区间DP:状态转移方程:dp[i] = min (dp[i], dp[j-1] + 1); dp[i] 表示前i个字符划分的最少回文串, 4 如果s[j] 到 s[i]是回文串,那么可以从dp[j-1] + 1递推过来 5 */ 6 #include <cstdio> 7 #include <cstring> 8 #include <algorithm> 9 #include <cmath&g

Uva 11584 - Partitioning by Palindromes dp

Problem H: Partitioning by Palindromes We say a sequence of characters is a palindrome if it is the same written forwards and backwards. For example, 'racecar' is a palindrome, but 'fastcar' is not. A partition of a sequence of characters is a list o

UVa 11584 Partitioning by Palindromes(DP 最少对称串)

题意  判断一个串最少可以分解为多少个对称串   一个串从左往后和从右往左是一样的  这个串就称为对沉串 令d[i]表示给定串的前i个字母至少可以分解为多少个对称串  那么对于j=1~i   若(i,j)是一个对称串  那么有  d[i]=min(d[i],d[j-1]+1)   然后就得到答案了 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N