判断字符串是否构成回文

Problem statement:

问题陈述:

Given string str find the minimum number of deletions such that the resultant string is a palindrome.

给定的字符串str找到最小的删除数,以使最终的字符串成为回文。

    Input:
Each input consists of the string str
Output:
Print the minimum number of characters
to be deleted to make the string a palindrome
Constraints:
String length will be under 1000

Example:

例:

    Input:
String str: "includecpni"
Output:
4

Explanation of Example:

示例说明:

    So, we need to find the longest palindromic subsequence
and delete the rest of the characters.
Here,
The longest palindromic sub-sequences are:
Inclcni
Incucni
Incdcni
Incecni
Incpcni
All these are of same length and are palindromes
So, minimum characters to delete are 4

Solution Approach:

解决方法:

We know what's a palindrome is? A palindrome is a string that is the same as its reverse order.

我们知道什么是回文吗? 回文是与其相反顺序相同的字符串。

That means to find the longest palindromic subsequence, we need to check the longest common subsequence between the string itself and its reverse string.

这意味着要找到最长回文子序列 ,我们需要检查字符串本身及其反向字符串之间的最长公共子序列。

So, basically

所以,基本上

    LPS(s) = LCS(s,reverse(s))
Where,
LPS(s) = longest palindromic subsequence for string s
LCS(s,reverse(s)) = Longest Common subsequence for
string s and reverse of string s

So, to find the longest palindromic subsequence:

因此,要找到最长的回文子序列:

  1. Find the reverse of the string

    查找字符串的反面

  2. Do an LCS between the string and its reverse string

    在字符串及其反向字符串之间执行LCS

  3. Result=string length-longest palindromic subsequence length

    结果=字符串长度-最长回文子序列长度

1) To find the reverse of the string

1)找到字符串的反面

    string reverse(string s,int n){
string p="";
for(int i=n-1;i>=0;i--)
p+=string(1,s[i]); //append characters from last
return p;
}

2) LCS between the string and its reverse string

2)字符串及其反向字符串之间的LCS

To detail how to find Longest Common subsequence b/w any two strings, go through this article, Longest Common subsequence

要详细了解如何找到任意两个字符串的最长公共子序列 ,请仔细阅读本文“ 最长公共子序列”

    L = length of the string,reverse of the string
Str1 = string itself
Str2 = Reverse of the string
1.  Initialize DP[l+1][l+1]  to 0
2.  Convert the base case of recursion:
for i=0 to l
DP[i][0]=0;
for i=0 to l
DP[0][i]=0;
Fill the DP table
for i=1 to l    //i be the subproblem length for the string
for j=1 to l //j be the subproblem length for reverse of the string
if(str1[i-1]==str2[j-1])
DP[i][j]=DP[i-1][j-1]+1;
else
DP[i][j]=max(DP[i-1][j],DP[i][j-1]);
end for
end for
4.  The final output will be DP[l][l]
Final step is to return minimum number of deletion which l-DP[l][l]
This will lead to the final result.

There is another way to find the longest palindromic subsequence, without using LCS. I wouldn't explain the detailed solution, rather try to think and implement your own.

还有另一种无需使用LCS即可找到最长回文子序列的方法。 我不会解释详细的解决方案,而是尝试考虑并实施自己的解决方案。

The recursion is,

递归是

    LPS(I,j) = LPS(i+1,j-1)+2
if
str1[i] == str[j]
Else
LPS(I,j) = max(LPS(i+1,j), LPS(i,j-1))

Convert the above recursion into DP while taking care of base cases.

将上述递归转换为DP,同时注意基本情况。

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
string reverse(string s, int n)
{string p = "";
for (int i = n - 1; i >= 0; i--)
p += string(1, s[i]);
return p;
}
//LCS of the strings
int LCS(string s1, string s2, int n)
{int dp[n + 1][n + 1];
for (int i = 0; i <= n; i++)
dp[0][i] = 0;
for (int i = 0; i <= n; i++)
dp[i][0] = 0;
for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {if (s1[i - 1] == s2[j - 1])
dp[i][j] = 1 + dp[i - 1][j - 1];
else
dp[i][j] = std::max(dp[i][j - 1], dp[i - 1][j]);
}
}
return dp[n][n];
}
int main()
{string s1;
cout << "Enter string:\n";
cin >> s1;
int n = s1.length();
//reverse of string
string s2 = reverse(s1, n);
//find LCS of the strings, result=n-LCS(s1,s2)
cout << "minimum characters to remove " << n - LCS(s1, s2, n) << endl;
return 0;
}

Output

输出量

Enter string:
includecpni
minimum characters to remove 4

翻译自: https://www.includehelp.com/icp/minimum-number-of-deletions-to-make-a-string-palindrome.aspx

判断字符串是否构成回文

判断字符串是否构成回文_构成字符串回文的最小删除数相关推荐

  1. python判断字符串是否包含大写字母_检查字符串中是否存在大写字母

    可以将any与生成器一起使用,以测试字符串是否包含大写字母testString = "abjKcf" print(any(x.isupper() for x in testStri ...

  2. 字符串查找字符出现次数_查找字符串作为子序列出现的次数

    字符串查找字符出现次数 Description: 描述: It's a popular interview question based of dynamic programming which ha ...

  3. java 字符串索引从0开始_无限字符串中的字符串的第一个索引-Java

    介绍 我有一个无限的字符串.这个字符串的长度在我们的想象中是无限的并且不能被限制,假设我们有一个这样的序列String: "123456789-" 数字9之后的点实际上表示下一个序 ...

  4. mysql中计算最大回撤_基金最大回撤算例(Java 1.8)

    1.测试数据源:长盛电子信息主题灵活配置混合(000063),2013-05-10~2016-04-15全部历史净值数据.数据净值曲线: 2016-4-17 18:26:30 上传 下载附件 (16. ...

  5. 机器人暑假班招生推文_「言情推文」妈妈这个机器人撩我!

    谁会不爱智商高还又贱又撩的小AI呢-图片来自网络,侵删 人机宝宝の第一篇言情推文(作者:昏古七)<含光>作者:酒小七 本文晋江全文免费!酒小七太太文笔剧情有保障!(就是湖南台魔改电视剧浪花 ...

  6. 怎么判断一个字符串的最长回文子串是否在头尾_最长回文字串/子序列问题(leetcode5,9,519)

    leetcode 5 最长回文子串 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad" 输出: " ...

  7. 判断输入的字符串是否为回文_刷题之路(九)--判断数字是否回文

    Palindrome Number 问题简介:判断输入数字是否是回文,不是返回0,负数返回0 举例: 1: 输入: 121 输出: true 2: 输入: -121 输出: false 解释: 回文为 ...

  8. 如何判断字符串已经被url编码_如何判断回文数?不要再将整数转为字符串来解决这个问题了...

    判断一个整数是否是回文数变成字符串判断其实很简单,但是效率方面很差.那么你能不将整数转为字符串来解决这个问题吗?下面我给大家介绍一下如何做. 什么是回文数 判断一个整数是否是回文数.回文数是指正序(从 ...

  9. java能否回文_如何使用Java查找字符串是否是回文?

    StringBuffer提供了一种名称为reverse()的方法,用于检查回文的一种方法是通过将所需的字符串作为参数传递给构造函数来创建StringBuffer对象. 使用reverse()方法反转对 ...

最新文章

  1. vb.net2019-hello,world
  2. JS经典面试题03-引用类型连续赋值a.x = a = { n: 2 }
  3. wxWidgets:wxMessageDialog类用法
  4. 感觉没睡好就..-shenmedoumeixie....
  5. sql where子查询5中字句的使用顺序
  6. 运用多种设计模式的综合案例_SpreadJS 纯前端表格控件应用案例:表格数据管理平台...
  7. RAC 中 ASM 实例名 与 节点的对应关系
  8. [译]A Simple CSS Animation Tutorial
  9. mysql备份管家婆_管家婆怎么恢复数据,备份数据
  10. [bzoj3812]主旋律
  11. 20行Python代码,轻松提取PPT文字到Word!
  12. CentOS配置samba文件共享服务
  13. 微信小程序开发者工具 无法加载以下来源的扩展程序 问题解决
  14. Java虚拟机(JVM)面试题(2022年总结最全面的面试题!!!)
  15. 论文阅读笔记《Adaptive Image-Based Visual Servoing Using Reinforcement Learning With Fuzzy State Coding》
  16. 《大话脑科学》之:熟练掌握十门外语之从入门到放弃之语言相关ERP
  17. 如何使用FFmpeg的解码器
  18. Java容器(集合)
  19. 年后跳槽如何准备?(前端方向)
  20. 怎么加网上的精准引流?精准引流有没有小技巧?

热门文章

  1. HTTP协议/RTSP协议/RTMP协议的区别
  2. 如何使用github搭建个人博客
  3. Vlc支持IE 360 低版本的Google浏览器
  4. Vue.js 相关知识(动画)
  5. 会话跟踪之Session
  6. 【DP】【期望】$P1850$换教室
  7. 上传图片或文件 方法一
  8. CSS :root 测试
  9. [转] Java, 使用 Reactor 进行反应式编程
  10. NoSQL入门第一天——NoSQL入门与基本概述