题解 | #查找两个字符串a,b中的最长公共子串#

重排链表

http://www.nowcoder.com/practice/3d281dc0b3704347846a110bf561ef6b

import java.io.; import java.util.; public class Main{ public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str; while((str = br.readLine())!=null){ String ss = br.readLine(); if(str.length()<ss.length()){ System.out.println(res(str,ss)); }else{ System.out.println(res(ss,str)); } } } public static String res(String s,String c){ char[] ch1 = s.toCharArray(); char[] ch2 = c.toCharArray(); int[][] ins = new int[ch1.length + 1][ch2.length + 1]; int max = 0; int start = 0; for (int i = 0; i < ch1.length; i++) { for (int j = 0; j < ch2.length; j++) { if(ch1[i]==ch2[j]){ ins[i+1][j+1] = ins[i][j]+1; if(ins[i+1][j+1]>max){ max = ins[i+1][j+1]; start = i-max; } } } } return s.substring(start+1,start+max+1); } }

我居南半坡 文章被收录于专栏

多刷题,积蓄力量,欢迎讨论

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务