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 ...
随机推荐
- sendmsg: no buffer space available
今天在将项目从虚拟机上移植到真实机器上面的时候,发现问题,总是不成功,最后判断是userspace的程序没有向kernel发送消息成功,因为无法触发kernel的行为,但是userspace显示正常. ...
- [LeetCode] 200. Number of Islands 解题思路
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- CoreText学习(一)Base Objects of Core Text
最近要做一个读入Word,PDF格式等的文件并且加以编辑的程序,本来以为使用Text Kit结合Text View来打开doc文件是完全没问题的,结果用了各种方法打开要么是数据是nil,要么打开的文字 ...
- 基准测试之netperf
使用 server端 [root@jiangyi01.sqa.zmf /home/ahao.mah/ALIOS_QA/tools/netperf] #netserver -p 10001 Starti ...
- JS-Array数组内置对象
直接上代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <ti ...
- 利用TreeSet给纯数字字符串排序
import java.util.Iterator;import java.util.TreeSet; /* * 给字符串中的数字排序 * String str = "10,2,11,1,3 ...
- 文件下载,带转码->pdf->swf
private String upload = "保存的路径"; //文件下载 public String download() { //初始化 this.initContext( ...
- [Angular 2]ng-class and Encapsulated Component Style2
Many Components require different styles based on a set of conditions. Angular 2 helps you style you ...
- Qt QString to char*
QString转换成char * 的时候,一定要定义一个QBateArray的变量.不能连写 How can I convert a QString to char* and vice versa ? ...
- (原创)初识cordova(一)
在公司做项目,发现有人在做大项目使用了cordova技术.做的是昆山的项目.之前听说过phonegap,也测试过,但是感觉效率不是很好,就没怎么研究,后来看他们做的项目还不错,于是想试一试. 搭建开发 ...