分治法求最长公共子序列和最长公共子串

一次上两个问题是因为我的错误。一开始想当然的觉得公共子序列就是公共子串 所以写的是求最长公共子串的算法。

实际

摘自百科的问题描述:

最长公共子序列,英文缩写为LCS(Longest Common Subsequence)。其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已知序列的最长公共子序列。而最长公共子串(要求连续)和最长公共子序列是不同的

 

上代码

public class StringOps
    {
        /// <summary>
        /// 主字符串1
        /// </summary>
        public string String1;
        /// <summary>
        /// 主字符串2
        /// </summary>
        public string String2;

        /// <summary>
        /// 子串
        /// </summary>
        public string SubString;

        /// <summary>
        /// GenMaxCommonSubStr用到的辅助字符串
        /// </summary>
        private string TempCommStr = null;


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="String1">字符串1</param>
        /// <param name="String2">字符串2</param>
        public StringOps(string String1,string String2)
        {
            this.String1 = String1;
            this.String2 = String2;
            SubString = string.Empty;
        }

        /// <summary>
        /// 获取最长公共子串函数,结果返回至SubString
        /// </summary>
        /// <param name="index1">下标1</param>
        /// <param name="index2">下标2</param>
        public void GenMaxCommonSubStr(int index1,int index2)
        {
            if (index1 >= String1.Length || index2 >= String2.Length)
            {
                if (TempCommStr != null && TempCommStr.Length > SubString.Length)
                {
                    SubString = TempCommStr;
                }
                TempCommStr = null;
                return;
            }
            if (String1[index1] != String2[index2])
            {
                if (TempCommStr != null && TempCommStr.Length > SubString.Length)
                {
                    SubString = TempCommStr;
                }
                TempCommStr = null;
                
            }
            else
            {
                if (TempCommStr != null)
                {
                    TempCommStr += String1[index1];
                }
                else
                {
                    TempCommStr = "" + String1[index1];
                }
                GenMaxCommonSubStr(index1 + 1, index2 + 1);
            }
            GenMaxCommonSubStr(index1 + 1, index2);
            GenMaxCommonSubStr(index1, index2 + 1);
        }
        
        /// <summary>
        /// 获取最长公共子序列函数
        /// </summary>
        /// <param name="index1">下标1</param>
        /// <param name="index2">下标2</param>
        /// <returns>公共子序列</returns>
        public string GetMaxCommonSubSeq(int index1, int index2)
        {
            if (index1 == -1 || index2 == -1)
            {
                return string.Empty;
            }
            if (String1[index1] == String2[index2])
            {
                string result = string.Empty + String1[index1];
                return GetMaxCommonSubSeq(index1 - 1, index2 - 1) + result;
            }
            else
            {
                string substr1 = GetMaxCommonSubSeq(index1, index2 - 1);
                string substr2 = GetMaxCommonSubSeq(index1 - 1, index2);
                return substr1.Length > substr2.Length ? substr1 : substr2;
            }
        }
    }

 

需要特别说明的是TempCommStr这个变量。把他声明称类的一个成员是因为 c#函数不支持静态成员 且没有找到类似的替代方案

posted on 2013-10-29 22:35  编号2784  阅读(874)  评论(0编辑  收藏  举报

导航