Codeforces 335B Palindrome
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的更多相关文章
- CF 335B - Palindrome 区间DP
335B - Palindrome 题目: 给出一个字符串(均有小写字母组成),如果有长度为100的回文子串,输出该子串.否则输出最长的回文子串. 分析: 虽然输入串的长度比较长,但是如果存在单个字母 ...
- Codeforces 486C Palindrome Transformation(贪心)
题目链接:Codeforces 486C Palindrome Transformation 题目大意:给定一个字符串,长度N.指针位置P,问说最少花多少步将字符串变成回文串. 解题思路:事实上仅仅要 ...
- codeforces 486C. Palindrome Transformation 解题报告
题目链接:http://codeforces.com/problemset/problem/486/C 题目意思:给出一个含有 n 个小写字母的字符串 s 和指针初始化的位置(指向s的某个字符).可以 ...
- Codeforces 159D Palindrome pairs
http://codeforces.com/problemset/problem/159/D 题目大意: 给出一个字符串,求取这个字符串中互相不覆盖的两个回文子串的对数. 思路:num[i]代表左端点 ...
- Codeforces 932G Palindrome Partition - 回文树 - 动态规划
题目传送门 通往???的传送点 通往神秘地带的传送点 通往未知地带的传送点 题目大意 给定一个串$s$,要求将$s$划分为$t_{1}t_{2}\cdots t_{k}$,其中$2\mid k$,且$ ...
- codeforces 486C Palindrome Transformation 贪心求构造回文
点击打开链接 C. Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes ...
- CF 335B. Palindrome(DP)
题目链接 挺好玩的一个题,1Y... #include <cstdio> #include <cstring> #include <iostream> using ...
- CodeForces 486C Palindrome Transformation 贪心+抽象问题本质
题目:戳我 题意:给定长度为n的字符串,给定初始光标位置p,支持4种操作,left,right移动光标指向,up,down,改变当前光标指向的字符,输出最少的操作使得字符串为回文. 分析:只关注字符串 ...
- 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 ...
随机推荐
- android TextView 添加下划线
android Textview加下划线 由于新做的一个项目要求有字体带下划线效果,当时看了下其实可以通过图片伪造出那种视觉效果.但是为了体现点技术含量,于是我想用Textview带下划线的效果.方法 ...
- [每日一题] 11gOCP 1z0-053 :2013-09-30 ASMCMD.......................................................8
转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/12206095 正确答案:BCD 为了使ASM文件管理更简单,Oracle提供了一个命令实用 ...
- 关闭myeclipse中jsp的校验功能
window--->preference--->Myeclipse--->Validation,取消下图红框中的选中状态.
- bat文件调用shutdown命令不生效问题原因
背景: 本人使用云桌面办公,但是用于登陆云桌面的终端运行卡顿,每次开机要20min才能登陆云桌面,所以: 1)在BIOS设置了定时开关,让终端提前开机 2)在系统上层,开机启动项增加一个bat文件(s ...
- ajax弹出窗口
提取自ZCMS的弹出框: 代替window.open.window.alert.window.confirm:提供良好的用户体验: 水晶质感,设计细腻,外观漂亮: 兼容ie6/7/8.firefox2 ...
- POJ 2175 Evacuation Plan (费用流,负环,消圈法,SPFA)
http://poj.org/problem?id=2175 Evacuation Plan Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- Face recognition using Histograms of Oriented Gradients
Face recognition using Histograms of Oriented Gradients 这篇论文的主要内容是将Hog算子应用到人脸识别上. 转载请注明:http://blog. ...
- Qt之加密算法
在写这篇文章之前,我曾反复思量关于加密的叫法是否准确,更为严格来说,应该是密码散列-将数据(如中英文字母.特殊字符)通过复杂的算法转换为另一种固定长度的值. QCryptographi ...
- C#文件的拆分与合并操作示例
C#文件的拆分与合并操作示例代码. 全局变量定义 ;//文件大小 //拆分.合并的文件数 int count; FileInfo splitFile; string splitFliePath; Fi ...
- 网页HTML1
表格表单 表格, <tabale> -------表格 <tr> --------------行 <td> -- ...