• C#动态规划查找两个字符串最大子串


     //动态规划查找两个字符串最大子串
            public static string lcs(string word1, string word2)
            {
                int max = 0;
                int index = 0;
                int[,] nums = new int[word1.Length + 1,word2.Length+1];
                for (int i = 0; i <= word1.Length; i++)
                {
                    for (int j = 0; j <= word2.Length; j++)
                    {
                        nums[i,j] = 0;
                    }
                }
     
     
                for (int i = 0; i <= word1.Length; i++)
                {
                    for (int j = 0; j <= word2.Length; j++)
                    {
                        if (i == 0 || j == 0)
                        {
                            nums[i, j] = 0;
                        }
                        else
                        {
                            if (word1[i - 1] == word2[j - 1])
                            {
                                nums[i,j] = nums[i - 1, j - 1] + 1;
                            }
                            else
                            {
                                nums[i, j] = 0;
                            }
                        }
                        if (max < nums[i, j])
                        {
                            max = nums[i, j];
                            index = i;
                        }
                    }
                }
                
                string str = "";
                if (max == 0)
                {
                    return "";
                }
                else 
                {
                    for (int i = index-max; i <= max; i++)
                    {
                        str += word2[i];
                    }
                    return str;
                }
            }
    好好学习,天天向上。
  • 相关阅读:
    08-蓝图&单元测试
    07-多对多操作
    JAVA 判断字符串是否可转化为JSONObject、JSONArray
    git常用命令
    线程和进程的一些基本概念
    JavaWeb学习总结(七):通过Servlet生成验证码及其应用 (BufferedImage类)
    JavaWeb学习总结(十):Session简单使用
    Ubuntu16.04 安装maven
    Ubuntu16.04 安装redis
    Ubuntu16.04 下安装tomcat
  • 原文地址:https://www.cnblogs.com/Zhengxue/p/6141495.html
Copyright © 2020-2023  润新知