http://codeforces.com/contest/335/problem/B

题意:

 给定一个长度不超过5*10^4的只包含小写字母的字符串,要求你求它的回文子序列,如果存在长度为100的回文子序列,那么只要输出长度为一百的回文子序列即可,否则输出它的最长回文子序列.

思路:如果n>=2600,那么就一定会有单个字符构成的100长度的回文子序列。

否则当n<2600时就可以n^2 DP,然后dfs输出

#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
char s[],b[];
int f[][],cnt,n,ans[];
int read(){
int t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void dfs(int l,int r){
if (l>r) return;
if (l==r){
b[++cnt]=s[l];
}else{
if (s[l]==s[r]){
b[++cnt]=s[l];
dfs(l+,r-);
b[++cnt]=s[r];
}else{
if (f[l+][r]>f[l][r-]) dfs(l+,r);
else dfs(l,r-);
}
}
}
int main(){
scanf("%s",s+);
n=strlen(s+);
if (n>=){
for (int i=;i<=n;i++){
ans[s[i]-'a']++;
if (ans[s[i]-'a']>=){
for (int j=;j<=;j++)
printf("%c",s[i]);
return ;
}
}
}
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
f[i][j]=;
for (int i=;i<=n;i++)
f[i][i]=;
for (int len=;len<=n;len++)
for (int i=;i+len-<=n;i++){
int j=i+len-;
if (s[i]==s[j]) f[i][j]=std::max(f[i][j],f[i+][j-]+);
else f[i][j]=std::max(f[i][j],std::max(f[i+][j],f[i][j-]));
}
dfs(,n);
if (cnt<=){
for (int i=;i<=cnt;i++)
printf("%c",b[i]);
}else{
for (int i=;i<=;i++)
printf("%c",b[i]);
for (int i=;i>=;i--)
printf("%c",b[i]);
}
return ;
}

Codeforces 335B Palindrome的更多相关文章

  1. CF 335B - Palindrome 区间DP

    335B - Palindrome 题目: 给出一个字符串(均有小写字母组成),如果有长度为100的回文子串,输出该子串.否则输出最长的回文子串. 分析: 虽然输入串的长度比较长,但是如果存在单个字母 ...

  2. Codeforces 486C Palindrome Transformation(贪心)

    题目链接:Codeforces 486C Palindrome Transformation 题目大意:给定一个字符串,长度N.指针位置P,问说最少花多少步将字符串变成回文串. 解题思路:事实上仅仅要 ...

  3. codeforces 486C. Palindrome Transformation 解题报告

    题目链接:http://codeforces.com/problemset/problem/486/C 题目意思:给出一个含有 n 个小写字母的字符串 s 和指针初始化的位置(指向s的某个字符).可以 ...

  4. Codeforces 159D Palindrome pairs

    http://codeforces.com/problemset/problem/159/D 题目大意: 给出一个字符串,求取这个字符串中互相不覆盖的两个回文子串的对数. 思路:num[i]代表左端点 ...

  5. Codeforces 932G Palindrome Partition - 回文树 - 动态规划

    题目传送门 通往???的传送点 通往神秘地带的传送点 通往未知地带的传送点 题目大意 给定一个串$s$,要求将$s$划分为$t_{1}t_{2}\cdots t_{k}$,其中$2\mid k$,且$ ...

  6. codeforces 486C Palindrome Transformation 贪心求构造回文

    点击打开链接 C. Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes ...

  7. CF 335B. Palindrome(DP)

    题目链接 挺好玩的一个题,1Y... #include <cstdio> #include <cstring> #include <iostream> using ...

  8. CodeForces 486C Palindrome Transformation 贪心+抽象问题本质

    题目:戳我 题意:给定长度为n的字符串,给定初始光标位置p,支持4种操作,left,right移动光标指向,up,down,改变当前光标指向的字符,输出最少的操作使得字符串为回文. 分析:只关注字符串 ...

  9. Codeforces 932G Palindrome Partition 回文树+DP

    题意:给定一个串,把串分为偶数段 假设分为$s_1,s_2,s_3....s_k$ 求满足$ s_1=s_k,s_2=s_{ k-1 }... $的方案数模$10^9+7$ $|S|\leq 10^6 ...

随机推荐

  1. 【HDOJ】1493 QQpet exploratory park

    超水的动态规划.最后要对概率求Sigma. #include <cstdio> #include <cstring> #include <cstdlib> #def ...

  2. 【转】VS2013中如何解决error C4996: 'fopen'问题

    原文网址:http://jingyan.baidu.com/article/ce436649fd61543773afd32e.html 今天编写控制台应用程序时出现如下错误 error C4996: ...

  3. cf202-div 1-B - Apple Tree:搜索,数论,树的遍历

      http://codeforces.com/contest/348/problem/B   B. Apple Tree time limit per test 2 seconds memory l ...

  4. bootstrap data

    bootstrap data 用法 <div data-role="page" data-last-value="43" data-hidden=&quo ...

  5. 【调试技巧】FireFox作为移动端的设置

    1.在地址栏输入“about:config”,按下回车进入设置菜单. 2.找到“general.useragent.override”,如果没有这一项,则点右键“新建”->“字符串”,输入这个字 ...

  6. DB2 错误编码 查询(二)(转)

    DB2 SQLSTATE 讯息 类代码 42 语法错误或访问规则违例表 32. 类代码 42:语法错误或访问规则违例 SQLSTATE 值   含义 42501 授权标识不具有对标识对象执行指定操作的 ...

  7. 10、Cocos2dx 3.0游戏开发找小三之容器篇:Vector、Map、Value

    重开发人员的劳动成果.转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27705613 容器 3.0版本号之前Cocos2d- ...

  8. [Falcor] Return the data from server

    <!-- index.html --> <html> <head> <!-- Do _not_ rely on this URL in production. ...

  9. 2014ACMICPC亚洲区域赛牡丹江现场赛之旅

    下午就要坐卧铺赶回北京了.闲来无事.写个总结,给以后的自己看. 因为孔神要保研面试,所以仅仅有我们队里三个人上路. 我们是周五坐的十二点出发的卧铺,一路上不算无聊.恰巧邻床是北航的神犇.于是下午和北航 ...

  10. Linux命令之用户与组管理

    介绍 Linux操作系统中,任何文件都归属某一特定的用户,而任何用户都隶属至少一个用户组.用户是否有权限对某文件进行访问.读写以及执行,受到系统严格约束的正式这种清晰.严谨的用户与用户组管理系统.在很 ...