判斷字符串是否構成回文_構成字符串回文的最小刪除數

判斷字符串是否構成回文

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

判斷字符串是否構成回文

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/380020.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/380020.shtml
英文地址,請注明出處:http://en.pswp.cn/news/380020.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

imul和idiv指令

imul 有符號乘法指令&#xff0c;分單操作數&#xff0c;雙操作數和但操作數 單操作數&#xff1a;此形式與mul指令使用完全相同&#xff0c;操作數乘以al、ax、或eax寄存器中的值&#xff0c;乘積分別存儲到ax、dx&#xff1a;ax或edx&#xff1a;eax中 執行指令&#xff1a…

Ajax的注冊應用

最近發現Ajax在用戶注冊表單和用戶登錄表單方面應用&#xff0c;最能體現Ajax的交互特點&#xff0c;因此又是寫了一個習作&#xff01; 演示效果 新開窗口地址&#xff1a; http://www.klstudio.com/demo/ajax/reg.htm 下載地址:http://www.klstudio.com/demo/ajax/reg.rar &…

Java——集合(模擬斗地主洗牌和發牌進行排序)

//改進版&#xff0c;沒有進行按牌的地位從小到大排序 package com.yy.test;import java.util.ArrayList; import java.util.Collections;public class Test2 {/*** * A&#xff1a;案例演示* 模擬斗地主洗牌核發牌&#xff0c;牌沒有排序* * 分析&#xff1a;* 1&#xff0c;…

應用程序控件

活動指示器 當任務或進程已經完成時&#xff0c;活動指示器就會消失。推薦您使用這種默認行為&#xff0c;因為用戶期望在有動作發生時看到活動指示器&#xff0c;而且他們會將靜止不動的活動指示器與停滯的進程聯想到一起。 要了解如何顯示網絡活動指示器&#xff0c;請參考UI…

離散數學與集合論_離散數學中的集合論和集合類型

離散數學與集合論集合論 (Set theory) The set is a well-defined collection of definite objects of perception or thought and the Georg Cantor is the father of set theory. A set may also be thought of as grouping together of single objects into a whole. The ob…

XADD和NEG命令

XADD 交換相加指令&#xff0c;先交換然后相加 比如說&#xff1a; xadd eax&#xff0c;ecx /* 相當于&#xff1a;先執行&#xff1a;xchg eax,ecx然后執行&#xff1a;add eax,ecx */此時eax2&#xff0c;ecx3&#xff0c;執行完&#xff1a;eax5&#xff0c;ecx2 neg …

Visual C# 2008+SQL Server 2005 數據庫與網絡開發--11.3.2 LINQ to SQL對數據庫建模

Visual Studio 2008版本中為LINQ to SQL提供了一個特別的設計器&#xff0c;使用這個設計器可以很方便的將數據庫可視化地轉換為LINQ to SQL對象模型。在LINQ to SQL中&#xff0c;設計器在關系數據庫的數據模型和開發語言之間建立一座橋梁。當應用程序運行時&#xff0c;LINQ …

Java——異常處理(鍵盤錄入一個整數,輸出其對于二進制)

例題&#xff1a; 鍵盤錄入一個int類型的整數&#xff0c;對其求二進制表現形式 如果錄入的整數過大&#xff0c;給予提示&#xff0c;錄入的整數過大&#xff0c;請重新錄入一個整數BigInteger 如果錄入的是小數&#xff0c;給予提示&#xff0c;錄入的是小數&#xff0c;請…

認清SQL_Server_2005的基于行版本控制的兩種隔離級別

--認清SQL_Server_2005的基于行版本控制的兩種隔離級別--By:zc_0101 Date:2010-03-31--快照隔離級別(snapshot)和已提交讀快照隔離級別(read committed snapshot)--特點&#xff1a;在這兩種隔離級別下&#xff0c;讀取數據時不再請求共享鎖&#xff0c;而且永遠不會與修改進程…

Java SecurityManager checkPermission()方法與示例

Syntax: 句法&#xff1a; public void checkPermission(Permission perm);public void checkPermission(Permission perm, Object cntxt);SecurityManager類的checkPermission()方法 (SecurityManager Class checkPermission() method) checkPermission() method is availa…

匯編test指令

功能&#xff1a;將兩個操作數進行邏輯與運算&#xff0c;并根據運算結果設置相關的標志位&#xff0c;并不改變操作數1和操作數2的值 test 操作數1&#xff0c;操作數2我們經常用test來判斷一個值是否為0&#xff0c;用法&#xff1a; test 操作數1&#xff0c;操作數1比如我…

CSS兼容IE/Firefox要點

首先我們說說firefox和IE對CSS的寬度顯示有什么不同&#xff1a; 其實CSS ’width’ 指的是標準CSS中所指的width的寬度&#xff0c;在firefox中的寬度就是這個寬度。它只包含容器中內容的寬度。而Internet Explorer ’width’則是指整個容器的寬度&#xff0c;包括內容&#x…

Java GregorianCalendar computeFields()方法與示例

GregorianCalendar類computeFields()方法 (GregorianCalendar Class computeFields() method) computeFields() method is available in java.util package. 在java.util包中提供了validateFields()方法 。 computeFields() method is used to compute the calendar fields and…

JS、JNS、JP(JPE)、JNP(JPO)指令詳解、從原理上解釋

JS 格式&#xff1a; js 地址當執行到JS指令時&#xff0c;如果標志位SF1&#xff0c;則跳轉到指定的地址&#xff0c;如果SF0&#xff0c;不跳轉 比如&#xff1a; cmp eax&#xff0c;ecx js 0040100c此時eax0&#xff0c;ecx1&#xff0c;執行完cmp命令&#xff0c;符號標…

zz如何保持專心

養成好習慣 養成在固定時間、固定地點專心學習工作的好習慣。 如果可能&#xff0c;在進入學習或者工作狀態前做一些小儀式&#xff0c;比如擺個姿勢&#xff0c;戴上學習帽什么的。就好像在運動前做準備活動一樣&#xff0c;給身體一個提示。讓頭腦做好準備 避免在學習前做什么…

Java——File類

一&#xff0c;File類的概述和構造方法 A&#xff1a;file類的概述 file類可以理解成一個路徑 文件夾或者是文件夾路徑 路徑分為絕對路徑和相對路徑 絕對路徑是一個固定的路徑&#xff0c;從盤符開始 這里的G&#xff1a;\TIM 就是一個絕對路徑&#xff0c;是一個固定的路…

Linux進程環境

一 main函數 當內核使用一個exec函數執行C程序時&#xff0c;在調用main函數之前先調用一個特殊的啟動例程&#xff0c;可執行程序將此例程指定為程序的起始地址。啟動例程從內核獲取命令行參數和環境變量&#xff0c;然后為調用main函數做好準備。 二 進程終止 進程終止的方式…

JO、JNO、JB、JNB命令詳解(從原理上)

JO 當執行到jo命令時&#xff0c;如果ZF標志位為1&#xff0c;則跳轉&#xff0c;反之不跳轉 add eax,ecx jo 00401000c此時eax7fff ffff &#xff0c;ecx0000 0001&#xff0c;執行完add命令&#xff0c;OF1&#xff0c;原因是eax存儲的最大值是7fffffff&#xff0c;再加1&a…

java 根據類名示例化類_Java類類getProtectionDomain()方法及示例

java 根據類名示例化類類class getProtectionDomain()方法 (Class class getProtectionDomain() method) getProtectionDomain() method is available in java.lang package. getProtectionDomain()方法在java.lang包中可用。 getProtectionDomain() method is used to return …

snagit 9.0注冊碼

8.0的注冊碼 A5CCU-RYNM4-C9ECC-5CWW9-B5R7B 5HCC5-4CCC9-NGXCM-XYDZ5-H6ER6 HLHAD-2CZLC-8XYDC-CC5CB-P289A D5DSC-WZCBM-JRHSC-QVTEV-TR7R8 snagit 9.0: name:Team Z.W.T sn:XMYU5-9CMBC-5SLBZ-DKML2-JE8M5 謝謝 name:Team Z.W.T sn: WDYMP-8ALRM-GVVV2-PH8VK-6MD27 Z…