CF 335B. Palindrome(DP)
挺好玩的一个题,1Y。。。
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int dp[][];
char s1[];
char s2[];
int o[];
char que[];
int main()
{
int i,j,len,a,b,num;
scanf("%s",s1);
len = strlen(s1);
for(i = ; i < len; i ++)
{
o[s1[i]-'a'] ++;
}
for(i = ; i < ; i ++)
{
if(o[i] >= )
{
for(j = ; j < ; j ++)
printf("%c",i+'a');
printf("\n");
return ;
}
}
for(i = ; i < len; i ++)
s2[i] = s1[len-i-];
for(i = ; i <= len; i ++)
{
for(j = ; j <= len; j ++)
{
if(s1[i-] == s2[j-])
dp[i][j] = dp[i-][j-] + ;
else
dp[i][j] = max(dp[i-][j],dp[i][j-]);
}
}
a = b = len;
num = ;
while(a != &&b != )
{
if(s1[a-] == s2[b-])
{
que[num++] = s1[a-];
a --;
b --;
}
else if(dp[a][b] == dp[a-][b])
a --;
else if(dp[a][b] == dp[a][b-])
b --;
}
if(num <= )
{
for(i = ; i < num; i ++)
{
printf("%c",que[i]);
}
}
else
{
for(i = ;i < ;i ++)
{
printf("%c",que[i]);
}
for(i = ;i >= ;i --)
{
printf("%c",que[i]);
}
}
printf("\n");
return ;
}
CF 335B. Palindrome(DP)的更多相关文章
- CF 335B - Palindrome 区间DP
335B - Palindrome 题目: 给出一个字符串(均有小写字母组成),如果有长度为100的回文子串,输出该子串.否则输出最长的回文子串. 分析: 虽然输入串的长度比较长,但是如果存在单个字母 ...
- CF 553A 组合DP
http://codeforces.com/problemset/problem/553/A A. Kyoya and Colored Balls time limit per test 2 seco ...
- CF 445A 简单DP
今天早上找一道题的bug,还是找不出来,下午刷了几道水题,晚上准备回家的事, 然后本来想打CF的,一看,数学场,不打了. 这道题的题意: 给出一个序列,每次你可以从这个序列里面选择一个数ak,删除,然 ...
- HDU 4632 CF 245H 区间DP(回文)
先说HDU 4632这道题,因为比较简单,题意就是给你一个字符串,然后给你一个区间,叫你输出区间内所有的回文子序列,注意是回文子序列,不是回文字串. 用dp[i][j]表示区间[i,j]内的回文子序列 ...
- uva 10453 - Make Palindrome(dp)
题目链接:10453 - Make Palindrome 题目大意:给出一个字符串,通过插入字符使得原字符串变成一个回文串,要求插入的字符个数最小,并且输出最后生成的回文串. 解题思路:和uva 10 ...
- UVA 10739 String to Palindrome(dp)
Problem H String to Palindrome Input: Standard Input Output: Standard Output Time Limit: 1 Second In ...
- CF 219D 树形DP
CF 219D [题目链接]CF 219D [题目类型]树形DP &题意: 给一个n节点的有向无环图,要找一个这样的点:该点到其它n-1要逆转的道路最少,(边<u,v>,如果v要到 ...
- POJ 3280 Cheapest Palindrome DP题解
看到Palindrome的题目.首先想到的应该是中心问题,然后从中心出发,思考怎样解决. DP问题通常是从更加小的问题转化到更加大的问题.然后是从地往上 bottom up地计算答案的. 能得出状态转 ...
- POJ 3280 Cheapest Palindrome(DP)
题目链接 被以前的题目惯性思维了,此题dp[i][j],代表i到j这一段变成回文的最小花费.我觉得挺难的理解的. #include <cstdio> #include <cstrin ...
随机推荐
- poj 2262【素数表的应用---判断素数】【哈希】
Goldbach's Conjecture Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35214 Accepted: ...
- [LeetCode] Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- 初识SQL Server Integration Service
SSIS(SQL Server Integration Service)是Microsoft 从SQL Server2005 以后发布的,现在一直跟随每个SQL server版本.它是Microsof ...
- git 常用的简单命令
git add . 会把当前目录中所有有改动的文件(不包括.gitignore中要忽略的文件)都添加到git缓冲区以待提交 git add * 会把当前目录中所有有改动的文件(包括.gitignore ...
- go sample - format
go sample - format package mainimport "fmt"import "os"type point struct { x, y i ...
- sublime text 2 安装emmet插件
一.添加插件之前先 下载Package Control 按 Ctrl+`(就是~这个键) 复制下面的代码 确认 重新启动sublime text2 import urllib2,os;pf='Pack ...
- hdu 4070 福州赛区网络赛J 贪心 ***
优先发路程最长的 #include<cstdio> #include<iostream> #include<algorithm> #include<cstri ...
- 使用AStyle进行代码格式化
转自:http://www.cnblogs.com/JerryTian/archive/2012/09/20/AStyle.html 在日常的编码当中,大家经常要遵照一些设计规范,如命名规则.代码格式 ...
- 对于c语言int类型和float,以及double类型表示范围的计算
首先说一下我原来错误的认识 int是32个bit, 如果我们把第一位理解为符号位,那么很显然int的范围是-(2^31-1)~2^31-1 但是实际上我们都知道int的最小值是-2^31次.. 为什么 ...
- java基本数据类型及相互间的转换
1.首先复习一下java的基本数据类型,见下图 2.比较他们的字节数 备注:1字节(Byte)=8位(Bit) 3.转换中的知识点 *java中整数类型默认的int类型:小数类型默认的double: ...