【UVa】Palindromic Subsequence(dp+字典序)
最长的很简单,将串翻转过来后求两个串的lcs就是答案。。
主要是字典序那里。。。
还是开string来比较吧。。
注意最后输出方案时用前半段推出后半段。(因为可能lcs时会重合。。。)
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mkpii make_pair<int, int>
#define pdi pair<double, int>
#define mkpdi make_pair<double, int>
#define pli pair<ll, int>
#define mkpli make_pair<ll, int>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
#define printarr1(a, b) for1(_, 1, b) cout << a[_] << '\t'; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; } const int N=1005;
char s1[N], s2[N];
int f[N][N], mn[N][N];
string p[N][N];
int main() {
while(~scanf("%s", s1+1)) {
CC(mn, 0x3f);
int n=strlen(s1+1);
for1(i, 1, n) s2[n-i+1]=s1[i];
for1(i, 1, n) for1(j, 1, n) {
if(s1[i]==s2[j]) f[i][j]=f[i-1][j-1]+1, p[i][j]=p[i-1][j-1]+s1[i];
else {
f[i][j]=max(f[i-1][j], f[i][j-1]);
if(f[i-1][j]==f[i][j-1]) p[i][j]=min(p[i-1][j], p[i][j-1]);
else if(f[i-1][j]>f[i][j-1]) p[i][j]=p[i-1][j];
else p[i][j]=p[i][j-1];
}
}
int len=(f[n][n]+1)>>1;
string ans1(p[n][n].begin(), p[n][n].begin()+len);
string ans2(ans1.rbegin()+(f[n][n]&1), ans1.rend());
printf("%s\n", (ans1+ans2).c_str());
}
return 0;
}
【UVa】Palindromic Subsequence(dp+字典序)的更多相关文章
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- UVA 11404 Palindromic Subsequence
Palindromic Subsequence Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA ...
- UVA 11404 五 Palindromic Subsequence
Palindromic Subsequence Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu ...
- LPS UVA 11404 Palindromic Subsequence
题目传送门 题意:求LPS (Longest Palidromic Subsequence) 最长回文子序列.和回文串不同,子序列是可以不连续的. 分析:1. 推荐->还有一种写法是用了LCS的 ...
- [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 ...
- 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
Longest Palindromic Subsequence 题解 题目来源:https://leetcode.com/problems/longest-palindromic-subsequenc ...
随机推荐
- Leet Code OJ 219. Contains Duplicate II [Difficulty: Easy]
题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...
- python发送QQ邮箱方法
import smtplib from email.mime.text import MIMEText mail_user = "user1@qq.com" mail_pwd = ...
- 【LeetCode】100. Same Tree (2 solutions)
Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...
- List、Set、Map常见集合遍历总结
Java中的集合有三大类,List.Set.Map,都处于java.util包中,List.Set和Map都是接口,不能被实例化,它们的各自的实现类可以被实例化.List的实现类主要有ArrayLis ...
- centos 7 搭建git远程仓储 免密登录
第一步.安装git服务 yum install git 第二步.创建git用户 adduser git 第三步开启公钥验证 vi /etc/ssh/sshd_config 讲文件中的 #PubkeyA ...
- 【打CF,学算法——二星级】Codeforces Round #313 (Div. 2) B. Gerald is into Art(水题)
[CF简单介绍] 提交链接:http://codeforces.com/contest/560/problem/B 题面: B. Gerald is into Art time limit per t ...
- [Jobdu] 题目1408:吃豆机器人
题目描述: 淘宝公司内部有许多新鲜的小玩具,例如淘宝智能机器人.小时候,大家都玩过那个吃豆子的游戏吧,这机器人就是按照这个游戏设计的,它会朝着豆子的方向行走.不过机器人还存在一个bug,他只会朝南和朝 ...
- GNU C编译器的gnu11和c11
国际标准组织发布c11后,gnu为自己的编译器发布两种标准gnu11和c11 gnu11:带gnu c扩展的c11标准,如果你的代码包含了typeof,__attribute__等等gnu的扩展,就必 ...
- C#多枚举值的写法与读法
首先,定义枚举的时候必须是2,4,8,16这种2的次方的值. using System; using System.Collections.Generic; using System.Linq; us ...
- samba 文件和目录权限控制
[laps_test] comment = laps_test path = /home/laps browseable = yes w ...