动态规划——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 ...
随机推荐
- jquery,javascript -设置某一ul下的li下的 a的属性
//javascriptvar ul = document.getElementById('ul); var as = ul.getElementsByTagName('a'); for(var i ...
- Creating a web application.
About creating web GIS applications As you learn and use ArcGIS for Server, you'll probably reach th ...
- 在WinForm应用程序中,使用选项卡控件来加载不同的Form界面!
TabPage tp=new TabPage(); your选项卡控件.Controls.Add(tp); From1 frm=new Form1(); frm.TopLevel = false; f ...
- cuda中时间用法
转载:http://blog.csdn.net/jdhanhua/article/details/4843653 在CUDA中统计运算时间,大致有三种方法: <1>使用cutil.h中的函 ...
- 关于Weblogic 10.3.1集群及调优经历
一. 集群 ·集群易于管理.灵活的负载平衡.较强的安全机制 ·配置前的规划 操作系统 硬件配置 角色 windows IP: 192.168.1.101:7001 AdminServer windo ...
- html的input输入框提示信息 点击隐藏
<input type="text" <!-- 文本框 --> name="textfield" value="请输入...& ...
- 利用set实现去重
最近读了一些有关于ES6的文章,觉得真是一个超级大的进步,就是不知道兼容性怎么样,鉴于我还在初学,先写个小例子练手,顺便时刻提醒自己要坚持学下去.未来的趋势肯定是替代es5没跑了. var arr=[ ...
- sphinx (coreseek)——2、区段查询实例
首先需要知道区段查询的定义: 索引系统需要通过主查询来获取全部的文档信息,一种简单的实现是将整个表的数据读入内存,但是这可能导致整个表被锁定并使得其他操作被阻止(例如:在MyISAM格式上的INSER ...
- RemoteViews嵌入ListView复杂布局
from http://blog.csdn.net/liliang497/article/details/8308313 主要函数 public void setRemoteAdapter (int ...
- 关于sql server 代理(已禁用代理xp)
由于有数据库在恢复,导致计划不能执行,先操作如下: 关闭数据库的服务..然后把数据库文件剪切出来.然后在起服务.进入SqlSever删除数据库.因为文件已经剪切走了.所以不会删除文件.再把数据库拷到M ...