URAL 1635. Mnemonics and Palindromes(DP)
本来用区间DP,3次方的复杂度,T了,看了看题解,降维,直接二次方的复杂度可以解。然后折腾一下输出路径。。终于过了。
#include <cstring>
#include <cstdio>
#include <string>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
char str[];
int dp[][];
int p[];
int pre[];
int flag[];
int main()
{
int i,len,j;
scanf("%s",str);
len = strlen(str);
for(i = ; i <= len; i ++)
{
dp[i][i] = ;
for(j = ;; j ++)
{
if(i-j+ < )break;
if(i+j >= len) break;
if(str[i-j] != str[i+j]) break;
dp[i-j][i+j] = ;
}
for(j = ;; j ++)
{
if(i-j+ < )break;
if(i+j >= len) break;
if(str[i-j+] != str[i+j]) break;
dp[i-j+][i+j] = ;
}
}
for(i = ; i < len; i ++)
p[i] = ;
p[] = ;
for(i = ; i < len; i ++)
{
if(dp[][i])
{
pre[i] = ;
p[i] = ;
}
}
for(i = ; i < len; i ++)
{
if(p[i] == ) continue;
for(j = ; j < i; j ++)
{
if(dp[j+][i])
{
if(p[i] > p[j] + )
{
pre[i] = j+;
p[i] = p[j]+;
}
}
}
}
printf("%d\n",p[len-]);
int a = pre[len-];
if(a != )
flag[a] = ;
a --;
while(p[len-] >= )
{
a = pre[a];
if(a != )
flag[a] = ;
a --;
p[len-] --;
}
for(i = ; i < len; i ++)
{
if(flag[i]) printf(" ");
printf("%c",str[i]);
}
printf("\n");
return ;
}
URAL 1635. Mnemonics and Palindromes(DP)的更多相关文章
- 回文串+回溯法 URAL 1635 Mnemonics and Palindromes
题目传送门 /* 题意:给出一个长为n的仅由小写英文字母组成的字符串,求它的回文串划分的元素的最小个数,并按顺序输出此划分方案 回文串+回溯:dp[i] 表示前i+1个字符(从0开始)最少需要划分的数 ...
- URAL 1635 Mnemonics and Palindromes
URAL 1635 思路:区间dp+贪心,先n^2处理出每段区间是否是回文串,然后贪心地找每一段1到i的最少分割. 代码: #include<bits/stdc++.h> using na ...
- Ural 1635 Mnemonics and Palindromes(DP)
题目地址:space=1&num=1635">Ural 1635 又是输出路径的DP...连着做了好多个了. . 状态转移还是挺简单的.要先预处理出来全部的回文串,tag[i] ...
- URAL1635. Mnemonics and Palindromes(DP)
链接 先初始化一下所有的回文串 再O(n*n)DP 输出路径dfs 刚开始存所有回文 ME了 后来发现用不着 改了改了OK了 数据还挺强 #include <iostream> #incl ...
- URAL - 1635 哈希区间(或者不哈希)+dp
题意: 演队在口试中非常不幸.在42道考题中,他恰好没有准备最后一道题,而刚好被问到的正是那道题.演队坐在教授面前,一句话也说不出来.但教授心情很好,给了演队最后一次通过考试的机会.他让这个可怜的学生 ...
- URAL 1658. Sum of Digits(DP)
题目链接 隔了一年零三个月,重新刷URAL,这题挺麻烦的输出路径.输出路径挺扯的,乱写了写乱改改就A了...我本来想用很靠谱,记录每一条路径的,然后输出最小的,结果Tle,然后我使劲水水又过了一组,发 ...
- URAL 1776 C - Anniversary Firework DP
C - Anniversary FireworkTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/c ...
- Ural 1073 Square Country (DP)
题目地址:Ural 1073 DP水题.也能够说是背包. #include <iostream> #include <cstdio> #include <string&g ...
- bzoj 1814 Ural 1519 Formula 1 插头DP
1814: Ural 1519 Formula 1 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 942 Solved: 356[Submit][Sta ...
随机推荐
- 【转载】Pyqt QSplitter分割窗口
转载来自: http://blog.sina.com.cn/s/blog_4b5039210100h3ih.html 分割窗口在应用程序中经常用到,它可以灵活分布窗口布局,经常用于类似文件资源管理器的 ...
- MVC中Form表单的提交
概述 Web页面进行Form表单提交是数据提交的一种,在MVC中Form表单提交到服务器.服务端接受Form表单的方式有多种,如果一个Form有2个submit按钮,那后台如何判断是哪个按钮提交的数据 ...
- 【翻译十七】java-并发之高性能对象
High Level Concurrency Objects So far, this lesson has focused on the low-level APIs that have been ...
- 【翻译四】java-并发之线程暂停
Pausing Execution with Sleep Thread.sleep causes the current thread to suspend execution for a speci ...
- 玩转SSRS第九篇---匿名访问的一个间接方法
SSRS是一个功能丰富的报表平台,我们可以在这个平台上实现各种不同需求的报表应用,所以这个平台也吸引了很多.net框架之外的技术,希望能在应用中引入SSRS的报表,比如JSP或者PHP页面,这个时候系 ...
- Golang gopath
golang 的gopath 至关重要,会影响到我们import package. golang 支持以相对路径的方式import,但是这种方式是不推荐的. 推荐的做法是在gopath中添加我们的项目 ...
- bzoj 1415 期望+记忆化搜索 ****
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAdkAAAIfCAIAAACzfDFhAAAgAElEQVR4nOy9bVwTW57vm5fnhed+Pn
- 顺序表C语言版
#include <stdio.h> /* * 顺序表最多输入N个数 */ #define N 10 #define OK 1 #define ERROR -1 struct sequeu ...
- SGU 275 To xor or not to xor 高斯消元求N个数中选择任意数XORmax
275. To xor or not to xor The sequence of non-negative integers A1, A2, ..., AN is given. You are ...
- Linux学习笔记(2)Linux学习注意事项
1 学习Linux的注意事项 ① Linux严格区分大小写 ② Linux中所有内容均以文件形式保存,包括硬件,如硬件文件是/deb/sd[a-p] ③ Linux不靠扩展名区分文件类型,但有的文件是 ...