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】1520 Anniversary party

    第二道树形DP,先是MLE.后来仅需改小邻接矩阵的第二个维度到30就过了. #include <cstdio> #include <cstring> #include < ...

  2. New Year Permutation(Floyd+并查集)

    Description User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to mak ...

  3. 学习DNS路上之CloudXNS

    使用CloudXNS已经有两年了,趁着他们现在做活动的机会也发表一下这两年来使用感受与CloudXNS的变化,也对我学习使用的一次总结. 简介 CloudXNS是北京快网开发的一套授权DNS系统, 用 ...

  4. jetty插件配置

    1.jetty maven 插件启动设置: Base directory:${project_loc} Goals:clean -Djetty.port=8080 jetty:run 2.jetty ...

  5. Apache Ant在Windows下配置环境变量

    Windows下ANT用到的环境变量主要有2个: ANT_HOME 和 PATH. 1..设置ANT_HOME指向ant的安装目录,如下: ANT_HOME = D:\soft\apache\apac ...

  6. brent ozar的sqlserver dba训练课程翻译——第二章:手动恢复数据库

    备份的唯一原因 备份的唯一原因是我们可以还原   当我第一次成为sqlserver数据库管理员,只要备份工作都能成功运行,我就会觉得一切都很好.我会查看sqlserver代理,保证那些作业都在运行,然 ...

  7. 修改cmd的字体

    通常打开的cmd默认的字体比较小,字体只有宋体和新宋体两种,如果要修改,需要通过修改注册表才行. 打开regedit后,找到如下路径HKEY_LOCAL_MACHINE\SOFTWARE\Micros ...

  8. c#序列化反序列化工具(json,binary,xml)

    using System; using System.Text; using System.IO; using System.Runtime.Serialization.Formatters.Bina ...

  9. 五、SolrJ、Request Handler

    什么是SolrJ 既然Solr是以单独的WebApp形式存在的,那么Solr理应提供与Solr通信的Api吧,对的,这就是SolrJ,既然与solr通信是通过url,那么其实我们也可以不用SolrJ, ...

  10. c++11 : Variadic Macros(变长参宏)

    Variadic macros are function-like macros that contain a variable number of arguments. Remarks   To u ...