UVA11404:Palindromic Subsequence
回文子串dp,最小字典序的话需要记录一下,注意是string型的,不能只记录一个字符,因为可能出现相等的情况
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
#define len first
#define str second
#define MAXN 1005
#define pii pair<int,int>
#define pis pair<int,string>
using namespace std;
pis dp[MAXN][MAXN];
pii g[MAXN][MAXN];
int n;
char s[MAXN];
int b[MAXN][MAXN];
int p[MAXN];
string e;
pis f(int L,int R){
if(b[L][R]){
return dp[L][R];
}
b[L][R]=;
pis& ans=dp[L][R];
if(L>R){
return ans=make_pair(,e);
}
if(L==R){
g[L][R]=make_pair(-,-);
string t(,s[L]);
return ans=make_pair(,t);
}
ans.len=-,ans.str=-;
if(s[L]==s[R]){
ans=f(L+,R-);
string t(,s[L]);t+=ans.str;
ans.len+=,ans.str=t;
if(L+<R)g[L][R]=make_pair(L+,R-);
else g[L][R]=make_pair(-,-);
}
if(ans<f(L,R-)){
ans=f(L,R-);
g[L][R]=make_pair(L,R-);
}
if(ans<f(L+,R)){
ans=f(L+,R);
g[L][R]=make_pair(L+,R);
}
return ans;
}
void print(int L,int R){
if(g[L][R]==make_pair(-,-)){
p[L]=p[R]=;
return;
}
if(g[L][R].first==L+&&g[L][R].second==R-){
p[L]=p[R]=;
}
print(g[L][R].first,g[L][R].second);
}
int main()
{
// freopen("data.in","r",stdin);
// freopen("my.out","w",stdout);
while(~scanf("%s",s+)){
memset(b,,sizeof(b));
memset(p,,sizeof(p));
n=strlen(s+);
for(int i=;i<=n;i++){
s[i]=-s[i];
}
f(,n);
print(,n);
for(int i=;i<=n;i++){
if(p[i])printf("%c",-s[i]);
}
printf("\n");
}
return ;
}
UVA11404:Palindromic Subsequence的更多相关文章
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- [leetcode-516-Longest Palindromic Subsequence]
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- [LeetCode] Longest Palindromic Subsequence 最长回文子序列
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- [Swift]LeetCode516. 最长回文子序列 | Longest Palindromic Subsequence
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- 516. Longest Palindromic Subsequence最长的不连续回文串的长度
[抄题]: Given a string s, find the longest palindromic subsequence's length in s. You may assume that ...
- [Leetcode] Longest Palindromic Subsequence
Longest Palindromic Subsequence 题解 题目来源:https://leetcode.com/problems/longest-palindromic-subsequenc ...
- LPS UVA 11404 Palindromic Subsequence
题目传送门 题意:求LPS (Longest Palidromic Subsequence) 最长回文子序列.和回文串不同,子序列是可以不连续的. 分析:1. 推荐->还有一种写法是用了LCS的 ...
- UVA 11404 Palindromic Subsequence
Palindromic Subsequence Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA ...
- [LeetCode] 516. Longest Palindromic Subsequence 最长回文子序列
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
随机推荐
- 1013团队Beta冲刺day6
项目进展 李明皇 今天解决的进度 进行前后端联动调试 明天安排 完善程序运行逻辑 林翔 今天解决的进度 服务器端发布消息,删除消息,检索消息,个人发布的action 明天安排 图片功能遇到问题,微信小 ...
- jQuery函数学习
函数:after(content) 功能:在每个匹配的元素后面添加html内容 返回:jQuery对象 参数:content (<Content>): Content to insert ...
- Scrum 冲刺 第一日
Scrum 冲刺 第一日 站立式会议 燃尽图 Alpha 阶段认领任务 明日任务安排 项目预期任务量 成员贡献值计算规则 今日贡献量 参考资料 站立式会议 返回目录 燃尽图 返回目录 Alpha 阶段 ...
- Flask jinja2 全局函数,宏
内置全局函数 dict()函数,方便生成字典型变量 {% set user = dict(name='Mike',age=15) %} <p>{{ user | tojson | safe ...
- PV & PVC - 每天5分钟玩转 Docker 容器技术(150)
Volume 提供了非常好的数据持久化方案,不过在可管理性上还有不足. 拿前面 AWS EBS 的例子来说,要使用 Volume,Pod 必须事先知道如下信息: 当前 Volume 来自 AWS EB ...
- 第一次制作和使用图标字体-IcoMoon
开题:之前就有所耳闻,最近两天第一次运用到图标字体.刚开始嘛,一脸懵逼的状态.成功运用之后就来记录一下使用过程咯! 1. 打开在线生成工具:https://icomoon.io/app/#/selec ...
- 算法题丨Remove Element
描述 Given an array and a value, remove all instances of that value in-place and return the new length ...
- 新概念英语(1-35)Our village
新概念英语(1-35)Our village Are the children coming out of the park or going into it ? This is a photogra ...
- Oracle 存储过程简单语法
一.无参数的存储过程 --创建存储过程create or replace procedure getdate as datetime varchar2(); begin select to_char( ...
- EasyUI datagrid动态生成列
任务描述:根据用户选择时间段,生成列数据,如图