Lightoj 1044 - Palindrome Partitioning (DP)

题目链接:

  Lightoj  1044 - Palindrome Partitioning

题目描述:

  给一个字符串,问至少分割多少次?分割出来的子串都是回文串。

解题思路:

  先把给定串的所有子串是不是回文串处理出来,然后用dp[i] 表示 从起点到串i的位置的最少分割次数,然后结合处理出来的回文串转移一下即可!

  还是好蠢哦!自己竟然感觉是一个区间DP,但是n又那么大,完全不是区间DP的作风啊!

 1 #include <cmath>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6
 7 using namespace std;
 8 typedef long long LL;
 9 const int maxn = 1010;
10 const int INF = 0x3f3f3f3f;
11
12 int dp[maxn];
13 bool a[maxn][maxn];
14 char str[maxn];
15 int main ()
16 {
17     int T;
18     scanf ("%d", &T);
19     for (int t=1; t<=T; t++)
20     {
21         scanf ("%s", str+1);
22         memset(a, false, sizeof(a));
23         int len = strlen (str+1);
24
25         for (int i=1; str[i]; i++)
26             a[i][i] = true;
27
28         for (int i=2; i<=len; i++)
29             for (int l=1; l+i<=len+1; l++)
30             {
31                 int r = l + i - 1;
32                 if ((r == l + 1 || a[l+1][r-1]) && str[l] == str[r]) a[l][r] = true;
33                 else    a[l][r] = false;
34             }
35
36         dp[0] = 0;
37         for (int i=1; i<=len; i++)
38         {
39             dp[i] = INF;
40             for (int j=1; j<=i; j++)
41                 if (a[j][i])    dp[i] = min (dp[i], dp[j-1]+1);
42         }
43         printf ("Case %d: %d\n", t, dp[len]);
44     }
45     return 0;
46 }
时间: 2024-07-31 23:40:27

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

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","

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

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 lo

D - Palindrome Partitioning (DP)

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

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

[LeetCode] Palindrome Partitioning II (DP)

Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab", Return 1 since the palindrome partitioning ["aa"

Atcoder Yet Another Palindrome Partitioning(状压dp)

Atcoder Yet Another Palindrome Partitioning 思路: 一个字符串满足条件的情况是奇数字母个数小于等于1,也就是异或起来是1<<j(0<=j<=25) 记mark是异或起来的值 状态转移: dp[mark]=dp[mark]+1; dp[mark]=min(dp[mark^(1<<j)]+1,dp[mark]);(0<=j<=25) 注意dp[0]被转移后可能会变成1,但是由它转移的需要dp[0]=0,所以每次记得把d

132. Palindrome Partitioning II (String; DP)

Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab",Return 1 since the palindrome partitioning ["aa",

LeetCode: Palindrome Partitioning II 解题报告

Palindrome Partitioning II Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab",Return 1 since the palindrome pa