链接

先初始化一下所有的回文串 再O(n*n)DP

输出路径dfs 刚开始存所有回文 ME了 后来发现用不着 改了改了OK了 数据还挺强

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define N 4010
#define INF 0xfffffff
int dp[N];
char s[];
int path[][],o[],q[][],k;
int g,flag,pp[];
void init()
{
int i,j;
k = strlen(s);
for(i = ; i < k ; i++)
{
int tt=;
while(i-tt>=&&i+tt<k&&s[i-tt]==s[i+tt])
{ g++;
int x1 = i-tt,x2 = i+tt;
o[x2]++;
q[x2][o[x2]] = x1;
tt++;
}
tt = ;
while(i-tt+>=&&i+tt<k&&s[i-tt+]==s[i+tt])
{
g++;
int x1 = i-tt+,x2 = i+tt;
o[x2]++;
q[x2][o[x2]] = x1;
tt++;
}
}
}
void dfs(int u,int d,int v)
{
if(flag) return ;
path[][v] = u;
path[][v] = d;
int i,j;
if(v==dp[k-])
{
for(i = dp[k-]; i > ; i--)
{
for(j = path[][i]; j <= path[][i]; j++)
printf("%c",s[j]);
printf(" ");
}
for(j = path[][]; j <= path[][]; j++)
printf("%c",s[j]);
puts("");
flag = ;
return ;
}
for(i = ; i <= o[u-] ; i++)
{
if(q[u-][i]==)
dfs(q[u-][i],u-,v+);
else
if(dp[u-]==dp[q[u-][i]-]+)
{
dfs(q[u-][i],u-,v+);
}
}
}
int main()
{
int i,j;
scanf("%s",s);
init();
for(i = ; i <= k ; i++)
dp[i] = INF;
dp[] = ;
for(i = ; i < k ;i++)
{
if(o[i])
{
for(j = ;j <= o[i] ; j++)
{
if(q[i][j]==)
{
dp[i] = ;
pp[] = i;
pp[] = q[i][j];
}
else if(dp[i]>dp[q[i][j]-]+)
{
dp[i] = dp[q[i][j]-]+;
pp[] = q[i][j];
pp[] = i;
}
}
}
}
printf("%d\n",dp[k-]);
dfs(pp[],pp[],);
return ;
}

URAL1635. Mnemonics and Palindromes(DP)的更多相关文章

  1. Ural 1635 Mnemonics and Palindromes(DP)

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

  2. LightOJ 1033 Generating Palindromes(dp)

    LightOJ 1033  Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  3. 【ARC064-F】【XSY2575】Rotated Palindromes(DP)(字符串)

    Description 然而,由于小C沉迷于制作游戏,他完全忘记了自己作为国家集训队的一员,还有156道作业题等他完成.还有一天作业就要截止了,而他一题还没有做.于是他赶紧挑了一道看起来最简单的题: ...

  4. 【UVa】Partitioning by Palindromes(dp)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=27&page=sh ...

  5. lightOJ 1047 Neighbor House (DP)

    lightOJ 1047   Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...

  6. UVA11125 - Arrange Some Marbles(dp)

    UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...

  7. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  8. 初探动态规划(DP)

    学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...

  9. Tour(dp)

    Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...

随机推荐

  1. JavaScript学习笔记(9)——JavaScript语法之流程控制

    javascript的流程控制语句与大部分类c语言一致.大致如下: 一.if if...else if...else if....else if....else..... 二.switch(变量){ ...

  2. 【小丸类库系列】Word操作类

    using Microsoft.Office.Interop.Word; using System; using System.Collections.Generic; using System.Dr ...

  3. javascript RegExp类型 学习小记

    在js里面,可以有两种方法来定义正则表达式,第一种是通过字变量的形式,第二种则是通过构造函数的形式: 1 字变量形式,格式是长这样子的 var expression = /pattern/flag p ...

  4. ZOJ 2624 Popo's Lamps(DP 记忆化搜索)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2624 题目大意:popo要将给定数量的灯变成自己想要的颜色,有一 ...

  5. error C2664 转换错误汇总[转]

    vs2005提示 error C2664: “CWnd::MessageBoxW”: 不能将参数 1 从“const char [17]”转换为“LPCTSTR”. 在用vs2005编写mfc程序的时 ...

  6. Passbook教程中生成pass时遇到的“Couldn't find a passTypeIdentifier in the pass”

    报错如下: 2014-03-28 15:19:17.990 signpass[6358:507] Couldn't find a passTypeIdentifier in the pass 解决方案 ...

  7. 使用WebClient上传文件并同时Post表单数据字段到服务端

    之前遇到一个问题,就是使用WebClient上传文件的同时,还要Post表单数据字段,一开始以为WebClient可以直接做到,结果发现如果先 Post表单字段,就只能获取到字段及其值,如果先上传文件 ...

  8. 多线程Demo

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. B树及2-3树的python实现

    B树(或称B-树)是一种适用于外查找的树,它是一种平衡的多叉树. 阶为M的B树具有下列结构特征: 1.树的根或者是一片树叶,或者其儿子数在2和M之间. 2.除根节点外的所有非树叶节点儿子数在┌M/2┐ ...

  10. Django路由

    一.路由流程 1. 用户浏览器发出请求后,通过根url设置,去找urlpattern变量.在setting.py中对 ROOT_URLCONF进行配置,以确定根URLconf(URL configur ...