题目传送门

 /*
题意:给出一个长为n的仅由小写英文字母组成的字符串,求它的回文串划分的元素的最小个数,并按顺序输出此划分方案
回文串+回溯:dp[i] 表示前i+1个字符(从0开始)最少需要划分的数量,最大值是i+1,即单个回文串;
之前设置ok[j][j+i] 判断从j到j+i的字符是否为回文串(注意两个for的顺序,为满足ok[j][j+i] = ok[j+1][j+i-1])
最后枚举找到最优划分点,用pre[i]记录前一个位置,print函数 输出空格
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std; const int MAXN = 4e3 + ;
const int INF = 0x3f3f3f3f;
int dp[MAXN];
int pre[MAXN];
bool ok[MAXN][MAXN];
char s[MAXN]; void print(int p)
{
if (pre[p] == -)
{
for (int i=; i<=p; ++i) printf ("%c", s[i]);
}
else
{
print (pre[p]); printf (" ");
for (int i=pre[p]+; i<=p; ++i) printf ("%c", s[i]);
}
} void work(void)
{
int len = strlen (s); for (int i=; i<len; ++i)
{
for (int j=; j+i<len; ++j)
{
if (i == ) ok[j][j+i] = true;
else if (i == ) ok[j][i+j] = (s[j] == s[j+i]);
else if (s[j] == s[j+i]) ok[j][j+i] = ok[j+][j+i-];
}
} for (int i=; i<len; ++i)
{
dp[i] = i + ; pre[i] = i - ;
if (ok[][i]) {dp[i] = ; pre[i] = -; continue;}
for (int j=i-; j>=; --j)
{
if (ok[j+][i])
{
if (dp[i] > dp[j] + )
{
dp[i] = dp[j] + ; pre[i] = j;
}
}
}
} printf ("%d\n", dp[len-]);
print (len-); puts ("");
} int main(void) //URAL 1635 Mnemonics and Palindromes
{
//freopen ("P.in", "r", stdin); while (scanf ("%s", s) == )
{
memset (ok, false, sizeof (ok));
memset (dp, , sizeof (dp));
memset (pre, , sizeof (pre)); work ();
} return ;
}

回文串+回溯法 URAL 1635 Mnemonics and Palindromes的更多相关文章

  1. URAL 1635 Mnemonics and Palindromes

    URAL 1635 思路:区间dp+贪心,先n^2处理出每段区间是否是回文串,然后贪心地找每一段1到i的最少分割. 代码: #include<bits/stdc++.h> using na ...

  2. Ural 1635 Mnemonics and Palindromes(DP)

    题目地址:space=1&num=1635">Ural 1635 又是输出路径的DP...连着做了好多个了. . 状态转移还是挺简单的.要先预处理出来全部的回文串,tag[i] ...

  3. URAL 1635. Mnemonics and Palindromes(DP)

    题目链接 本来用区间DP,3次方的复杂度,T了,看了看题解,降维,直接二次方的复杂度可以解.然后折腾一下输出路径..终于过了. #include <cstring> #include &l ...

  4. Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning)

    Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning) 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. ...

  5. LeetCode刷题笔记-回溯法-分割回文串

    题目描述: 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab"输出:[ ["aa", ...

  6. Palindrome - URAL - 1297(求回文串)

    题目大意:RT   分析:后缀数组求回文串,不得不说确实比较麻烦,尤其是再用线段数进行查询,需要注意的细节地方比较多,比赛实用性不高......不过练练手还是可以的.   线段数+后缀数组代码如下: ...

  7. 分割回文串 · Palindrome Partitioning

    [抄题]: 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 给出 s = "aab",返回 [ ["aa", & ...

  8. 131. 分割回文串 javascript实现

    给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa",&quo ...

  9. (回文串)leetcode各种回文串问题

    题目一:最长连续回文子串. 问题分析:回文串顾名思义表示前后读起来都是一样,这里面又是需要连续的.分析这个问题的结构,可以想到多种方法.暴力解决的方式,2层循环遍历得出各个子串,然后再去判断该子串是否 ...

随机推荐

  1. QQ微信与智能家电连接一起 小马哥"连接一切"野心凸显

    昨日,彭博社对于海南举行的腾讯全球合作伙伴大会进行了报道,文章指出腾讯公司正在发力移动端,将其即时通讯工具QQ和微信与烤箱.电视.空调等其他家电连接在一起.小马哥"连接一切"的野心 ...

  2. [Effective JavaScript 笔记] 第13条:使用立即调用的函数表达式创建局部作用域

    function wrapElements(a){ var res=[],i,n; for(i=0,n=a.length;i<n;i++){ res[i]=function(){return a ...

  3. Unity 3D学习之 Prime31 Game Center插件用法

    http://momowing.diandian.com/post/2012-11-08/40041806328 It's my life~: 为app 连入Game Center 功能而困扰的朋友们 ...

  4. 纹理缓存(Texture Cache)

    纹理缓存是将纹理缓存起来方便之后的绘制工作.每一个缓存的图像的大小,颜色和区域范围都是可以被修改的.这些信息都是存储在内存中的,不用在每一次绘制的时候都发送给GPU. CCTextureCache C ...

  5. 基本二叉搜索树的第K小元素

    #include<stdio.h> #include<stdlib.h> typedef struct node *btlink; struct node { int data ...

  6. javascript quine

    javascript有一些奇怪的性质,恩,比如说,非常容易写一个quine,即自己输出自己代码的东西. function a(){console.log(a.toString()+";a() ...

  7. PHP 遍历目录

    $dir = $_SERVER['DOCUMENT_ROOT'].'/test'; //var_dump($dir);exit; function my_scandir($dir) { $files ...

  8. Segment Tree Query I & II

    Segment Tree Query I For an integer array (index from 0 to n-1, where n is the size of this array), ...

  9. C++代码重用——包含

    #ifndef PAIRS_H #define PAIRS_H #include <iostream> #include <valarray> template <cla ...

  10. asp.net文本编辑器FCKeditor使用方法详解

    文本编辑器的使用: 1.FCKeditor的官方网站是:http://www.fckeditor.net/download  目前最新的FCKeditor.Net_2.6.9版本. 请在此页下载:ht ...