codefores-335B-求最长的回文序列
来源:互联网 发布:曾经红过的网络歌曲 编辑:程序博客网 时间:2024/05/16 11:51
B. Palindrome
time limit per test
2 secondsmemory limit per test
256 megabytesinput
standard inputoutput
standard outputGiven a string s, determine if it contains any palindrome of length exactly 100 as a subsequence. If it has any, print any one of them. If it doesn't have any, print a palindrome that is a subsequence of s and is as long as possible.
Input
The only line of the input contains one string s of length n (1 ≤ n ≤ 5·104) containing only lowercase English letters.
Output
If s contains a palindrome of length exactly 100 as a subsequence, print any palindrome of length 100 which is a subsequence of s. If sdoesn't contain any palindromes of length exactly 100, print a palindrome that is a subsequence of s and is as long as possible.
If there exists multiple answers, you are allowed to print any of them.
Sample test(s)
input
bbbabcbbb
output
bbbcbbb
input
rquwmzexectvnbanemsmdufrg
output
rumenanemur
代码:
#include <iostream>#include <string>#include <stdio.h>#include <algorithm>using namespace std;#define MAXN 111111char s[MAXN];int N = 0;#define MAXM 111int dp[MAXN][111], prefi[MAXN][111], prefj[MAXN][111];int last[MAXN][MAXM];int best = 0;char tmp[MAXN];char ans[MAXN];int main() {scanf("%s", s);N = strlen(s);for (int i = 0; i < 26; i ++)last[0][i] = -1;for (int i = 0; i < N; i ++) {for (int j = 0; j < 26; j ++) {last[i + 1][j] = last[i][j];last[i + 1][s[i] - 'a'] = i;}}for (int j = 0; j < MAXN; j ++) {dp[0][j] = -1;}dp[0][0] = N;for (int i = 0; i < N; i ++)for (int j = 0; j < 55; j ++)if (dp[i][j] >= i) {int len = 2 * j + (dp[i][j] > i);if(len <= 100 && best < len) {best = len;int ii = i, jj = j;int n = 0;while(jj) {int ni = prefi[ii][jj], nj = prefj[ii][jj];if (nj != jj) tmp[n++] = s[ni];ii = ni; jj = nj;}int A = 0;for (int k = 0; k < n; k ++)ans[A++] = tmp[n - 1 - k];if (dp[i][j] > i) ans[A++] = s[i];for (int k = 0; k < n; k ++)ans[A++] = tmp[k];}if(dp[i][j] > dp[i+1][j]) {prefi[i+1][j] = i; prefj[i+1][j] = j;dp[i+1][j] = dp[i][j];}if(last[dp[i][j]][s[i]-'a'] > dp[i+1][j+1]) {prefi[i+1][j+1] = i; prefj[i+1][j+1] = j;dp[i+1][j+1] = last[dp[i][j]][s[i]-'a'];}}ans[best] = '\0';printf("%s\n", ans);return 0;}
- codefores-335B-求最长的回文序列
- 409. Longest Palindrome 求最长的回文序列
- 求一个字符串中的最长回文序列
- 利用最长公子序列求回文字符串
- 动态规划求最长回文子序列
- 求给定字符串中最长回文序列
- 求数组b的最长不减子序列长度
- 求str最长回文子序列是求这个原字符串和它反转字符串的最长公共子序列。
- 动态规划 LCS 求两个序列A,B中所有的最长公共子序列
- 动态规划 LCS 求两个序列A,B中全部的最长公共子序列
- 51Nod1092 回文字符串-----求最长公共子序列
- 求LPS——最长回文子序列
- 算法导论--动态规划算法--求最长回文序列
- 求最长回文序列(pat 1040. Longest Symmetric String)
- 2017百度校园招聘编程题 删除字符构造最长回文字符串(求最长公共子序列的变种)
- 判断是否包含回文、判断是否是回文、求最长回文、得到最长回文的长度
- 最长回文子序列的java解法
- 计算字符串的最长回文子序列
- rpm命令管理
- openstack quantum多机测试
- Sphinx在windows下安装使用[支持中文全文检索]
- HTML超出文本多行截取代码
- WPF背后的动机
- codefores-335B-求最长的回文序列
- 关闭android程序里所有的activity
- YouTube图片幻灯片分享技巧
- 图形的设计和HTML和的CSS
- 图标元素库FontAwesome
- Java 读取文件方法大全
- Java回调函数使用
- 好的网站的四大原则
- 如何能使网站高效的运行