求两个字符串的最长公共字串(dp)

package com.cjh.dp;


import com.sun.swing.internal.plaf.basic.resources.basic;

public class Dp2 {
	public static void main(String[] args) {
		//求两个字符串的最长公共子串
		method("itheima","thema");
	}

	private static void method(String a, String b) {
		int max = 0;
		// TODO Auto-generated method stub
		int[][] dp = new int[b.length()][a.length()];
		for (int i = 0; i < b.length(); i++) {
			for (int j = 0; j < a.length(); j++) {
				if (a.charAt(j) == b.charAt(i)) {
					if(i == 0 || j == 0) {
						dp[i][j] = 1;
					}else {
						dp[i][j] = dp[i - 1][j - 1] + 1;
					}
					max = Math.max(max, dp[i][j]);
				}else {
					dp[i][j] = 0;
				}
			}
		}
		System.out.println(max);
	}
}

你可能感兴趣的:(算法,Java,java,算法,数据结构)