最长回文序列举例_最长回文序列

最长回文序列举例

Problem statement:

问题陈述:

Given a string find the length of longest palindromic subsequence.

给定一个字符串,找到最长回文子序列的长度。

Example:

例:

    Input:
    abaccaabbaa
    Output:
    6

    Input:
    aaa
    Output:
    3

Solution:

解:

Let,

让,

L(i,j)=length of subsequence from index i to j having length j-i+1
L(i,i)=1 ... base case

L(i,j)=从索引i到长度为j-i + 1的j的子序列的长度
L(i,i)= 1 ...基本情况

It's obvious because each single letter is itself a palindrome and thus every string has palindrome of length 1.

很明显,因为每个字母本身都是回文,因此每个字符串都具有长度为1的回文。

L(i,i) = substring starting from i ending at i, i.e, a single letter<

L(i,i)=从i到i结束的子字符串 ,即单个字母<

Now,

现在,

L(i,j)=2+L(i+1,j-1) if string[i]=string[j]
L(i,j)=max⁡(L(i+1,j),L(i,j-1)) if string[i]≠string[j]

如果string [i] = string [j],则L(i,j)= 2 + L(i + 1,j-1)
如果string [i]≠string [j],则L(i,j)=max⁡(L(i + 1,j),L(i,j-1))

This is quite obvious : If string[i]=string[j] then it just extends whatever is value for L(i+1,j-1). Else we take the longest possible value so far.

这是很明显的:如果string [i] = string [j],那么它只会扩展L(i + 1,j-1)的值 。 否则,我们将获得尽可能长的价值。

We can actually convert these recursive definitions to our DP matrix.

实际上,我们可以将这些递归定义转换为我们的DP矩阵。

  1. Let the DP matrix to be L[n][n] where n is string length. Reasons behind the dimensions are maximum possible start and end index value can be n-1

    令DP矩阵为L [n] [n] ,其中n是字符串长度。 尺寸背后的原因是最大可能的开始和结束索引值可以是n-1

    Initialize DP matrix with 0.

    用0初始化DP矩阵。

  2. For base case,

    对于基本情况,

    For i=0:n-1
        L[i][i]=1
    
    
  3. For i=2:n //i is substring length
        For j=0:n-i
            start=j;
            end=j+i-1;
            IF(i==2 && s[start]==s[end]) //two letter palindromes
                L[start][end]=2;
            ELSE IF(s[start]==s[end])
                L[start][end]=2+L[start+1][end-1]; //extend length
            ELSE
                //choose max between two possible substring output
                L[start][end]=max(L[start+1][end],L[start][end-1]); 
        END For
    END For
    
    
  4. L[0][n-1] is our final result (0 starting index, n-1 end index)

    L [0] [n-1]是我们的最终结果( 0个开始索引, n-1个结束索引)

Example with explanation:

带有说明的示例:

    Let's see the example1.
    abaccaabbaa

    Longest palindromic sub sequence can be:
    abaccaba (subsequence is not similar to substring)
    Thus longest one has length 8
    Now check how actually program worked
    For 1 length
    L[i][i] =1
    So the left to right diagonal actually becomes 1
    Now for each possible length of 2 to n
    We start from index 0 and keep incrementing
    We can actually review first few iterations
    So for i=2
    -------------------------------------------------

    J=0
    Start =0
    End =1 //(i+j-1)
    I==2 but s[0]≠s[1]
    L[0][1]=0 //no updating
    -------------------------------------------------

    J=1
    Start =1
    End =2//(i+j-1)
    I==2 but s[1]≠s[2]
    L[1][2]=0 //no updating
    -------------------------------------------------

    Same for j=2
    J=3
    Start =3
    End =4 //(i+j-1)
    I==2 and s[3]=s[4]
    L[3][4]=2 //updating here

Similarly we can carry on iterations for all length and ultimately L[0][n-1] gives the final result

同样,我们可以对所有长度进行迭代,最终L [0] [n-1]给出最终结果

C++ implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;

void print(vector<int> a,int n){
	for(int i=0;i<n;i++)
		cout<<a[i]<<" ";
	cout<<endl;
}

int max(int a,int b){
	return (a>b)?a:b;
}


int LPS(string s){
	int n=s.length();
	int start,end;

	int L[n][n];
	memset(L,0,sizeof(L)); //initialize to 0

	for(int i=0;i<n;i++) //base case of single letter string
		L[i][i]=1;

	for(int i=2;i<=n;i++){ //i is length of string
		for(int j=0;j<n-i+1;j++){ //j=starting index
			start=j;
			end=j+i-1;
			if(i==2 && s[start]==s[end]) //two length palindrome
				L[start][end]=2;
			else if(s[start]==s[end]) //if s[start]=s[end]
				L[start][end]=2+L[start+1][end-1]; //extend
			else
				//take max of possible two substring output
				L[start][end]=max(L[start+1][end],L[start][end-1]);
		}
	}
	return L[0][n-1]; //final result
}

int main()
{
	int t,n,item;
	string s;
	
	cout<<"Enter input string\n";
	cin>>s;
	
	cout<<"Longest palindromic sub-sequence is: ";
	cout<<LPS(s)<<endl;
	
	return 0;
}

Output

输出量

Enter input string
abaccaabbaa
Longest palindromic sub-sequence is: 8


翻译自: https://www.includehelp.com/icp/longest-palindromic-subsequence.aspx

最长回文序列举例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值