Partitioning by Palindromes

题意:

给定一个字符串,求能分成最小几个回文串

分析:简单dp dp[i]前i个字符能分成的最小数量 dp[i]=min(dp[i],dp[j-1]+1) (j-i 是回文串)

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll  INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod =  1000000007;
int dp[1010];
char s[1010];
int judge(int x,int y){
    while(x<y){
        if(s[x]!=s[y])return 0;
        x++;y--;
    }
    return 1;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%s",(s+1));
        int len=strlen(s+1);
        for(int i=1;i<=len;++i)
            dp[i]=INF;
            dp[0]=0;
        for(int i=1;i<=len;++i)
            for(int j=1;j<=i;++j)
            if(judge(j,i)){
                dp[i]=min(dp[i],dp[j-1]+1);
            }
        printf("%d\n",dp[len]);
    }
return 0;
}
时间: 2024-10-18 02:09:43

Partitioning by Palindromes的相关文章

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

/*---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]

UVa 11583 Partitioning by Palindromes

 Description 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 charact

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

Partitioning by Palindromes Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 11584 1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 using namespace std; 5 6 char a[1

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)

粗略的复杂度是L^3,长度最大是1000,,没敢做,之后发现其实这个复杂度的系数也不大,可以过,而且很快. dp[j] = dp[i - 1] + 1 (if(str[i] ~ str[j]为回文) 14327451 11584 Partitioning by Palindromes Accepted C++ 0.052 2014-10-09 09:33:17 #include<set> #include<map> #include<stack> #include<

区间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 最少对称串)

题意  判断一个串最少可以分解为多少个对称串   一个串从左往后和从右往左是一样的  这个串就称为对沉串 令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

Dynamic Programming [Beginner]~A - 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 of one or more disjoint non-empty groups