动态规划——G 回文串
Description
As an example, by inserting 2 characters, the string "Ab3bd" can be transformed into a palindrome ("dAb3bAd" or "Adb3bdA"). However, inserting fewer than 2 characters does not produce a palindrome.
Input
Output
Sample Input
5
Ab3bd
Sample Output
2
题目大意:一串字符,从左往右读和从右往左读是完全一样的。
解题思路:
这个题目可以转化为求最长公共子串的问题,就是将原字符串逆转,然后求原字符串和逆转字符串的最长公公子串,最后要注意c数组要定义成short型,否则会超内存 程序代码:
#include<cstdio>
#include <cstring>
using namespace std;
#define MAX 5010
char a[MAX],b[MAX];
short c[MAX][MAX];
int max(int x,int y)
{
return x>y?x:y;
}
int main()
{
int n;
scanf("%d",&n);
scanf("%s",a);
for(int i=n-;i>=;i--)
b[n-i-]=a[i];
b[n]='\0';
memset(c,,sizeof(c));
for(int i=;i<n;i++)
for(int j=;j<n;j++)
{
if(a[i]==b[j])
c[i+][j+]=c[i][j]+;
else
{
if(c[i+][j]>c[i][j+])
c[i+][j+]=c[i+][j];
else c[i+][j+]=c[i][j+];
}
}
printf("%d\n",n-c[n][n]); return ;
}
动态规划——G 回文串的更多相关文章
- 集训第五周动态规划 G题 回文串
Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...
- hdu 1159 Palindrome(回文串) 动态规划
题意:输入一个字符串,至少插入几个字符可以变成回文串(左右对称的字符串) 分析:f[x][y]代表x与y个字符间至少插入f[x][y]个字符可以变成回文串,可以利用动态规划的思想,求解 状态转化方程: ...
- 集训第五周动态规划 H题 回文串统计
Hrdv is interested in a string,especially the palindrome string.So he wants some palindrome string.A ...
- 动态规划——H 最少回文串
We say a sequence of characters is a palindrome if it is the same written forwards and backwards. Fo ...
- UVA-11584 Partitioning by Palindromes 动态规划 回文串的最少个数
题目链接:https://cn.vjudge.net/problem/UVA-11584 题意 给一个字符串序列,问回文串的最少个数. 例:aaadbccb 分为aaa, d, bccb三份 n< ...
- 【CPLUSOJ】【动态规划】最短回文串
题目链接 [问题描述] 如果一个字符串正过来读和倒过来读是一样的,那么这个字符串就被称作回文串.例如abcdcba,abcddbca就是回文串,而abcdabcd不是. 你要解决的问题是:对于任意一个 ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Longest Palindromic Substring 最长回文串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 删除部分字符使其变成回文串问题——最长公共子序列(LCS)问题
先要搞明白:最长公共子串和最长公共子序列的区别. 最长公共子串(Longest Common Substirng):连续 最长公共子序列(Longest Common Subsequence,L ...
随机推荐
- SimpleDateFormat使用详解
http://blog.csdn.net/gubaohua/article/details/575488 public class SimpleDateFormat extends DateForma ...
- angularjs-ngModel传值问题
js NiDialog.open({ windowClass: '', backdrop: 'static', keyboard: false, templateUrl: '/static/tpl/a ...
- PageMapAdapter MapAdapter (续webServices)
public class PageMapAdapter extends XmlAdapter<PageMapConverter, IPage<Map<String, Object&g ...
- JavaScript - 测试 jQuery
测试 JavaScript 框架库 - jQuery 引用 jQuery 如需测试 JavaScript 库,您需要在网页中引用它. 为了引用某个库,请使用 <script> 标签,其 s ...
- PHP算法 《图》 之 理论基础
转载自:http://www.cnblogs.com/skywang12345/p/3691463.html Ⅰ 图的基本概念 1. 图的定义 定义:图(graph)是由一些点(vertex)和这些点 ...
- 【POJ1151】【扫描线+线段树】Atlantis
Description There are several ancient Greek texts that contain descriptions of the fabled island Atl ...
- Queue学习
Queue在Python中可以算作是一种容器,但是他和list,set,dict不一样. 1. Queue不是Python内置类型.它在Queue模块中定义. 2. 它不是iterator容器,他不能 ...
- APP被Rejected 的各种原因翻译(转)
原文:http://www.cnblogs.com/sell/archive/2013/02/16/2913341.html Terms and conditions(法律与条款) 1.1 As a ...
- paramiko SSH 模块简单应用。
目的:需要ssh链接到Linux主机,执行telnet 命令,抓回显匹配制定内容. ssh --->执行telnet到本地端口--->执行类似 ls 的命令.匹配命令执行后的特定回显字段. ...
- zoj3839-Poker Face
#include<cstdio>int n;void P(int i,int j,int n,int f){ if(i==n){ for(int k=1;k<=n;k++)print ...