最长公共子串(连续)
2015-08-12 17:29
363 查看
题目标题:计算两个字符串的最大公共字串的长度,字符不区分大小写 |
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); String str = s.nextLine(); String[] ss = str.split(" "); String s1 = ss[0]; String s2 = ss[1]; int result = longSubstring(s1,s2); System.out.println(result); } public static int longSubstring(String s1,String s2){ if(s1==null||s1.equals("")||s2==null||s2.equals("")){ return 0; } s1 = s1.toUpperCase(); s2 = s2.toUpperCase(); int len1 = s1.length(); int len2 = s2.length(); int[][] result = new int[len2][len1]; for(int i=0;i<len1;i++){ if(s2.charAt(0)==s1.charAt(i)){ result[0][i] = 1; } } for(int j=0;j<len2;j++){ if(s1.charAt(0)==s2.charAt(j)){ result[j][0] = 1; } } for(int i=1;i<len2;i++){ for(int j=1;j<len1;j++){ if(s1.charAt(j)==s2.charAt(i)){ result[i][j] = result[i-1][j-1]+1; } } } int max = 0; for(int i=1;i<len2;i++){ for(int j=1;j<len1;j++){ if(result[i][j]>max){ max = result[i][j]; } } } return max; } }
思路参考博客:
http://www.cnblogs.com/zhangchaoyang/articles/2012070.html
相关文章推荐
- 解决SVG跨浏览器兼容性问题
- SQL Server 触发器
- 嵌入式 select、poll、epoll之间的区别总结
- ldconfig
- 将svn插件和maven插件和findbugs插件插入到myeclipse10
- 图的深搜与广搜
- 带余除法专题
- 按照特定轨迹遍历字符串图
- Linux运行二进制文件时出现 can't execute :Permission denied
- GNU组织的 gcc 和 g++ 的区别详解
- UNITY3D学习笔记15
- 字节 字符
- 最小生成树prim算法实现
- HDU 2089 不要62 (数位DP)
- 多台web服务器之间共享session
- Django的模板系统
- mac系统怎么给文件夹加密?mac文件夹加密教程
- COJS 1752. [BOI2007]摩基亚Mokia
- 递归:全排列的java实现
- UITabBar