日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2023/12/1 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 判断字符串是否构成回文_构成字符串回文的最小删除数 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

判斷字符串是否構(gòu)成回文

Problem statement:

問題陳述:

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

給定的字符串str找到最小的刪除數(shù),以使最終的字符串成為回文。

Input:Each input consists of the string strOutput:Print the minimum number of characters to be deleted to make the string a palindromeConstraints: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:InclcniIncucniIncdcniIncecniIncpcniAll these are of same length and are palindromesSo, 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 sLCS(s,reverse(s)) = Longest Common subsequence for string s and reverse of string s

So, to find the longest palindromic subsequence:

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

  • Find the reverse of the string

    查找字符串的反面

  • Do an LCS between the string and its reverse string

    在字符串及其反向字符串之間執(zhí)行LCS

  • Result=string length-longest palindromic subsequence length

    結(jié)果=字符串長度-最長回文子序列長度

  • 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

    要詳細(xì)了解如何找到任意兩個(gè)字符串的最長公共子序列 ,請仔細(xì)閱讀本文“ 最長公共子序列”

    L = length of the string,reverse of the stringStr1 = string itselfStr2 = Reverse of the string1. Initialize DP[l+1][l+1] to 02. Convert the base case of recursion:for i=0 to lDP[i][0]=0;for i=0 to lDP[0][i]=0;Fill the DP table for i=1 to l //i be the subproblem length for the stringfor j=1 to l //j be the subproblem length for reverse of the stringif(str1[i-1]==str2[j-1]) DP[i][j]=DP[i-1][j-1]+1;elseDP[i][j]=max(DP[i-1][j],DP[i][j-1]);end forend 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即可找到最長回文子序列的方法。 我不會(huì)解釋詳細(xì)的解決方案,而是嘗試考慮并實(shí)施自己的解決方案。

    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.

    將上述遞歸轉(zhuǎn)換為DP,同時(shí)注意基本情況。

    C++ Implementation:

    C ++實(shí)現(xiàn):

    #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];elsedp[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 stringstring 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

    判斷字符串是否構(gòu)成回文

    總結(jié)

    以上是生活随笔為你收集整理的判断字符串是否构成回文_构成字符串回文的最小删除数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。