您的位置:首页 > 其它

最长回文序列(返回长度)

2015-10-23 00:15 260 查看
int solve(const string &s,int l,int r)
{
if(l==r)
        return 1;
    if(s[l]==s[r]){
if(l+1==r)
    return 2;
else
    return solve(s,l+1,r-1)+2;
    }
return max(solve(s,l+1,r),solve(l,r-1));
}

// Everay single character is a palindrom of length 1
L(i, i) = 1 for all indexes i in given sequence

// IF first and last characters are not same
If (X[i] != X[j])  L(i, j) =  max{L(i + 1, j),L(i, j - 1)}

// If there are only 2 characters and both are same
Else if (j == i + 1) L(i, j) = 2

// If there are more than two characters, and first and last
// characters are same
Else L(i, j) =  L(i + 1, j - 1) + 2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: