zoukankan      html  css  js  c++  java
  • [ Algorithm ] LCS 算法 动态规划解决

    LCS算法简述:

    求出一个未知序列 s0 ,如果是两个或多个已知序列s1、s2、s3......sn的子序列,

    则 未知序列s0 称为已知S序列集的最长公共子序列。

    而最长公共子串(要求连续)和最长公共子序列是不同的。

    算法原理,不一一细讲。

    code 0 :

    public static int Get_LCS_Length(string query, string keyword)
    {
        if (query == keyword) return query.Length;
    
        int MaxLength = 0;
        int QueryLength = query.Length;
        int KeyWordLength = keyword.Length;
    
        int[] QueryMatrix = new int[QueryLength >= KeyWordLength ? QueryLength : KeyWordLength];
    
        if ((String.IsNullOrEmpty(query)) || (String.IsNullOrEmpty(keyword))) return 0;
        if (query.IndexOf(keyword) > -1) return KeyWordLength;
    
        for (int i = 0; i < QueryLength; i++)
        {
            for (int j = 0; j < KeyWordLength; j++)
            {
                if (query[i] == keyword[j])
                {
                    if ((i == 0) || (j == 0)) QueryMatrix[j] = 1;
                    else QueryMatrix[j] = QueryMatrix[j - 1] + 1;
                }
    
                if (QueryMatrix[j] > MaxLength) MaxLength = QueryMatrix[j];
            }
        }
        return MaxLength;
    }

    作者:文道
    出处:http://www.cnblogs.com/VincentDao
    关于作者:北漂猴子一枚
    本文版权归作者文道所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接
    如有问题,可以通过邮件my_interface@126.com联系我,非常感谢。

  • 相关阅读:
    UIDynamicBehavior的简单使用:接球小游戏
    UI控件初始化问题:initWithFrame和initWithCoder、aweakFromNib的执行
    ICP、MRR、BKA优化
    consul运维入门
    crontab问题
    archer运维相关问题
    git安装及错误处理
    galera
    线性回归
    python数据分析
  • 原文地址:https://www.cnblogs.com/VincentDao/p/3170781.html
Copyright © 2011-2022 走看看