c++求两个字符串的最长公共子序列and子串

https://blog.csdn.net/ggdhs/article/details/90713154

#include <iostream>
using namespace std;

int main() {
        char str1[] = "helloworld";
        char str2[] = "loop";
        int arr[11][5] = {0};
        for(uint32_t i=1; i<11;i++) {
                cout << str1[i-1] << endl;
                for(uint32_t j=1; j<5;j++) {
                        cout<< str2[j-1];
                        if (str1[i-1] ==  str2[j-1]) {
                                arr[i][j] = arr[i-1][j-1] + 1;
                        } else {                   // 公共子序列
                                arr[i][j] = max(arr[i-1][j], arr[i][j-1]);
                                // 公共字串                        // arr[i][j] = 0;
                        }
                }
                cout << endl;
        }

        for(uint32_t i=0; i<11;i++) {
                for(uint32_t j=0; j<5;j++) {
                        cout<<arr[i][j];
                }
                cout<<endl;
        }

        return 0;
}

  

原文地址:https://www.cnblogs.com/sailrancho/p/11781311.html

时间: 2024-08-29 09:24:29

c++求两个字符串的最长公共子序列and子串的相关文章

动态规划之----求两个字符串的最长公共子序列

package DongtaiGuihua; /** * Created by hunk on 2015/9/13. */public class LongestCommonSubstring { public static void main(String[] args){ String str1="BDCABA"; String str2="ABCBDAB"; System.out.println(findMaxCommonSubstring(str1,str2

算法导论中如何求两个字符串的最长公共子序列

#include<iostream> #include<string> using namespace std; const int max_length=10; void print(int b[max_length][max_length],string X,int i,int j); void LCS(string X,string Y,int b[max_length][max_length],int m,int n ) { int **c=new int*[m+1]; f

求两个字符串的最长公共子串——Java实现

要求:求两个字符串的最长公共子串,如"abcdefg"和"adefgwgeweg"的最长公共子串为"defg"(子串必须是连续的) public class Main03{ // 求解两个字符号的最长公共子串 public static String maxSubstring(String strOne, String strTwo){ // 参数检查 if(strOne==null || strTwo == null){ return null

求解两个字符串的最长公共子序列

一,问题描述 给定两个字符串,求解这两个字符串的最长公共子序列(Longest Common Sequence).比如字符串1:BDCABA:字符串2:ABCBDAB 则这两个字符串的最长公共子序列长度为4,最长公共子序列是:BCBA 二,算法求解 这是一个动态规划的题目.对于可用动态规划求解的问题,一般有两个特征:①最优子结构:②重叠子问题 ①最优子结构 设 X=(x1,x2,.....xn) 和 Y={y1,y2,.....ym} 是两个序列,将 X 和 Y 的最长公共子序列记为LCS(X,

求三个字符串的最长公共子序列LCS(A,B,C)

LCS(A,B,C)!=LCS(A,LCS(B,C)) 反例: abcd abcde abced LCS(B,C)求出来可能是abce或者abcd dp[i][j][k]表示A[0...i],B[0...j],C[0...k]的LCS 转移方程: if (a[i]==b[j]&&b[j]==c[k]) dp[i][j][k]=dp[i-1][j-1][k-1]+1; else dp[i][j][k]=max(max(dp[i][j][k], dp[i-1][j][k]), max(dp[i

动态规划求两个序列的最长公共子序列

摘录:http://blog.chinaunix.net/uid-26548237-id-3374211.html 1.序列str1和序列str2 ·长度分别为m和n: ·创建1个二维数组L[m.n]: ·初始化L数组内容为0 ·m和n分别从0开始,m++,n++循环: - 如果str1[m] == str2[n],则L[m,n] = L[m - 1, n -1] + 1: - 如果str1[m] != str2[n],则L[m,n] = max{L[m,n - 1],L[m - 1, n]}

[URAL-1517][求两个字符串的最长公共子串]

Freedom of Choice URAL - 1517 Background Before Albanian people could bear with the freedom of speech (this story is fully described in the problem "Freedom of speech"), another freedom - the freedom of choice - came down on them. In the near fu

【java】求两个字符串的最长公共子串

这个是华为OJ上的一道题目.首先,如果我们用java写代码,华为OJ有以下三条规则需遵守,否则编译无法通过或者用例无法通过,规则如下: (1)一定不可以有包名: (2)主类名只能为Main: (3)不可以输出与结果无关的信息. 好了,按照以上规则,我们写出来的代码如下(此代码不是最优的,只是用来记录华为OJ上java代码的书写规则): import java.util.Scanner; public class Main { public static void main(String[] ar

求两个字符串的最长公共子串

public static String findLongestOfTheSame(String s1,String s2) { char[] c1=s1.toCharArray(); char[] c2=s2.toCharArray(); int l1=c1.length; int l2=c2.length; int count=0,maxLength=0,start=0,end=0; boolean hasTheSame=false; for(int i=0;i<l1;i++) { coun