最长公共子串问题

本文介绍两种字符串匹配算法:最长公共子串及最长公共子序列的实现方法。通过示例代码展示了如何使用暴力搜索和动态规划解决这两个问题,并提供了一种直观的理解方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.util.HashMap;
import java.util.Map;

/**
 * Created by tangtang on 15/6/22.
 */
public class LongSubString {


    public static void main(String[] args)
    {
        String src="abcdefgh";
        String dest="cbdef";


    }


    /**
     * 第一种方法  暴力求解 这个 很简单
     *
     */
    public static String longSubString(String src,String dest)
    {
        String maxString="";

        for (int i=0;i<src.length();i++)
        {
            for (int j=0;j<dest.length();j++)
            {
                int temp;
                if (dest.charAt(j)==src.charAt(i))
                {

                    //这里开始记录
                    String tempMax=dest.charAt(j)+"";
                    int l=1;
                    for (int h=j+1;h<dest.length();h++,l++)
                    {
                        if ((i+l)<src.length()&&src.charAt(i+l)==dest.charAt(h))
                        {
                            tempMax+=dest.charAt(h);
                        }else {
                            break;
                        }

                    }

                    if (maxString.length()<tempMax.length())
                    {
                        maxString=tempMax;
                    }
                }
            }
        }

        return maxString;
    }



    /**
     * 动态规划
     * 思想:很简单  分成str1长度个子问题
     *  如果  s1[i]!=s1[j] 则把当前len清0   longest用于记录总最大长度
     *  否则 m n 各自++ 严格的说 这个和上边的是一样的 不同实现而已
     */
    public static int  longestCommonSubstring(String str1,String str2)
    {
        return compute(str1.toCharArray(), str2.toCharArray());
    }

    public static int  compute(char[] str1,char[] str2)
    {
        int len1=str1.length;
        int len2=str2.length;

        int start1=-1;
        int start2=-1;


        int longest=0;

        for (int i=0;i<str1.length;i++)
        {
            int m=i;
            int n=0;
            int length = 0;

            while (m<len1&&n<len2)
            {
                if (str1[m]!=str2[n])  //满足条件
                {
                    length=0;  //不满足清0
                }else {
                    length++;
                    if (longest<length)
                    {
                        longest=length;
                        start1 = m - longest + 1;
                        start2 = n - longest + 1;
                    }
                }
            }
        }

        // shift string2 to find the longest common substring
        for (int j = 1; j < len2; ++j)
        {
            int m = 0;
            int n = j;
            int length = 0;
            while (m < len1 && n < len2)
            {
                if (str1[m] != str2[n])
                {
                    length = 0;
                }
                else
                {
                    ++length;
                    if (longest < length)
                    {
                        longest = length;
                        start1 = m - longest + 1;
                        start2 = n - longest + 1;
                    }
                }

                ++m;
                ++n;
            }
        }
        System.out.printf("from %d of str1 and %d of str2, compared for %d times\n", start1, start2);
        return longest;
    }







}



动态规划 最长子序列问题
/**
 * Created by tangtang on 15/6/22.
 */
public class LongestSubSequence {


    public static void main(String[] args)
    {
        String[] x = {"","A", "B", "C", "B", "D", "A", "B"};
        String[] y = {"", "B", "D", "C", "A", "B", "A"};


        int[][] b = getLength(x, y);
        Display(b, x, x.length-1, y.length-1);
    }

    public static  int[][] getLength(String[] str1,String[] str2)
    {
        int [][]b=new int[str1.length][str2.length];
        int c[][]=new int[str1.length+1][str2.length+1];

        for (int i=0;i<str1.length;i++)
        {
            c[i][0]=0;
        }

        for (int j=0;j<str2.length;j++)
        {
            c[0][j]=0;
        }

        for (int i=1;i<str1.length;i++)
        {
            for (int j=1;j<str2.length;j++)
            {
                if (str1[i]==str2[j])
                {
                    b[i][j]=0;
                    c[i][j]=c[i-1][j-1]+1;
                }else if (c[i-1][j]>c[i][j-1])
                {
                    b[i][j]=1;
                    c[i][j]=c[i-1][j];
                }else {
                    b[i][j]=2;
                    c[i][j]=c[i][j-1];
                }
            }
        }

        return  b;
    }


    public static void Display(int[][] b, String[] x, int i, int j)
    {
        if (i==0||j==0)
            return;

        if (b[i][j]==0)
        {
            Display(b, x, i-1, j-1);
            System.out.print(x[i] + " ");
        }else if (b[i][j]==1)
        {
            Display(b,x,i-1,j);
        }else {
            Display(b,x,i,j-1);
        }



    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TangGeeA

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值