首页 > 其他分享 >LightOJ - 1044 Palindrome Partitioning(DP)

LightOJ - 1044 Palindrome Partitioning(DP)

时间:2023-04-07 13:02:31浏览次数:35  
标签:Partitioning Palindrome const int LightOJ 划分 include dp 回文


题目大意:给你一个字符串,要求你对字符串进行划分,使得划分出来的子串都是回文串,且子串数量达到最小

解题思路:用dp[i]表示前i个字符划分成回文串,需要划分成多少个部分
接着枚举j,如果[i,j]回文,那么dp[i] = min(dp[i], dp[j - 1] + 1)

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1010;
char str[N];
int dp[N];
int cas = 1;

bool judge(int i, int j) {
    while (i <= j) {
        if (str[i] != str[j]) return false;
        i++; j--;
    }
    return true;
}

void solve() {
    scanf("%s", str);
    int len = strlen(str);
    for (int i = 0; i < len; i++) {
        dp[i] = INF;
        for (int j = 0; j <= i; j++) {
            if (judge(j, i)) {
                if (j == 0) dp[i] = 1;
                else dp[i] = min(dp[i], dp[j - 1] + 1);
            }
        }
    }
    printf("Case %d: %d\n", cas++, dp[len - 1]);
}
int main() {
    int test;
    scanf("%d", &test);
    while (test--) solve();
    return 0;
}


标签:Partitioning,Palindrome,const,int,LightOJ,划分,include,dp,回文
From: https://blog.51cto.com/u_10970600/6176013

相关文章

  • LightOJ - 1300 Odd Personality(边双连通+奇圈判定)
    题目大意:给出一张无向图,要求找出符合条件的点条件如下:从该点出发,经过一定数量的边,又回到该点,经过的边不能重复经过,且经过的边的数量为奇数解题思路:要回到原点,且不能重复经过边,只能在边双连通分量中找了接着要判断的是有多少个点,只要边双连通分量中有奇圈,那么这个连通分量中的所......
  • LightOJ - 1400 Employment(婚姻稳定问题)
    题目大意:在一个party上,有N个男的,N个女的,要求你将其配对,使其满足1.男生u和女生v还没配对2.他们喜欢对方的程度都大于喜欢各自当前舞伴的程度如果出现了2的情况,他们就会抛下当前的舞伴,另外组成一对解题思路:这题的话,就是婚姻稳定问题,他的解决方法是,男士不断的求婚,而女士不断的拒......
  • LightOJ - 1063 Ant Hills(割点)
    题目大意:求无向图中,有多少个割点解题思路:模版题了#include<cstdio>#include<cstring>#include<vector>#include<stack>usingnamespacestd;#definemax(a,b)((a)>(b)?(a):(b))#definemin(a,b)((a)<(b)?(a):(b))constintMAXNODE=10005;constintM......
  • LightOJ - 1041 Road Construction(最小生成树)
    题目大意:给你N条边,看能否形成最小生成树,如果存在,输出值,不存在,另外输出解题思路:模版题#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<map>#include<string>#include<iostream>usingnamespacestd;constintMAXNOD......
  • 洛谷P1217 [USACO1.5]回文质数 Prime Palindromes
    #include<bits/stdc++.h>usingnamespacestd;inta,b;boolzs(intx){if(x%2>0){for(inti=3;i<x;i+=2)if(x%i==0)returnfalse;returntrue;}elsereturnfalse;}boolhws(intx......
  • 《Spectral Partitioning Residual Network With Spatial Attention Mechanism for Hy
    论文作者:XiangrongZhang,ShouwangShang,XuTang,etal.论文发表年份:2021模型简称:SPRN发表期刊:IEEETransactionsonGeoscienceandRemoteSensing论文链接:Sci-Hub......
  • 【APIO2014】Palindromes
    先说一下自己的SAM做法:看到回文串我们首先考虑对以下字符串建立SAM:正串+特殊字符1+特殊字符2+反串。这样也许能有一点用。晚上睡觉前我考虑的是对于正串的endpos在反串中......
  • CF245H Queries for Number of Palindromes
    对字符串s,多次询问,给你两个数L和R,问在字符串区间l到r的字串中,包含多少回文串。 #include<iostream>#include<queue>#include<cstring>#defineIOSstd::ios::syn......
  • Palindrome Linked List
    SourceGivenasinglylinkedlistofcharacters,writeafunctionthatreturnstrueifthegivenlistispalindrome,elsefalse.题解1-使用辅助栈根据栈的......
  • hdu-1513-Palindrome
    PalindromeTimeLimit:4000/2000MS(Java/Others)    MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):4052    AcceptedSubmission(s):1377......