BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / java / #18453同步于 2011/5/20
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖

求大牛讲解一下这个程序

nuckid
2011/5/20镜像同步7 回复
求两个字符串的公共最大子串....... public class getLCStringTest { public static String getLCSring(String str1,String str2){ StringBuffer str=new StringBuffer(); int i,j; int len1,len2; len1=str1.length(); len2=str2.length(); int maxlen=len1>len2?len1:len2; int[] max=new int[maxlen]; int[] maxIndex=new int[maxlen]; int[] c=new int[maxlen]; for(i=0;i<len2;i++){ for(j=len1-1;j>=0;j--){ if(str2.charAt(i)==str1.charAt(j)){ if(i==0||j==0)c[j]=1; else c[j]=c[j-1]+1; }else{ c[j]=0; } if(c[j]>max[0]){ max[0]=c[j]; maxIndex[0]=j; for(int k=1;k<maxlen;k++){ max[k]=0; maxIndex[k]=0; } }else if (c[j]==max[0]) { for(int k=1;k<maxlen; k++){ if(max[k]==0){ max[k]=c[j]; maxIndex[k]=j; break; } } } } } for(j=0;j<maxlen;j++){ if(max[j]>0){ str.append(str1.substring(maxIndex[j]-max[j]+1, maxIndex[j]+1)); } } return str.toString().trim(); } public static void main(String[] args) { String str1="英国独立音乐尽管“石玫瑰”引导了英国独立音乐的跳舞潮流涅槃科特"; String str2="尽管“石玫瑰”引导了英国独立音乐英国独立音乐的跳舞潮流"; System.out.println(getLCSring(str1, str2));; } }
订阅后,新回复会通过你的通知中心匿名送达。
7 条回复
a206206机器人#1 · 2011/5/20
这个是算法啊
nuckid机器人#2 · 2011/5/21
是个什么思路呢? 【 在 a206206 的大作中提到: 】 : 这个是算法啊 : --
cm536机器人#3 · 2011/5/21
动态规划longest common subsequence 搜一下吧
pq机器人#4 · 2011/5/21
http://www.doc88.com/p-5780300881.html
buptlong机器人#5 · 2011/5/21
大悲剧 算法一窍不通 【 在 nuckid (Kid Rock) 的大作中提到: 】 : 求两个字符串的公共最大子串....... : public class getLCStringTest { : public static String getLCSring(String str1,String str2){ : ...................
Kazehaya机器人#6 · 2011/5/21
同。。 【 在 buptlong (楚|火属性的小龙|八卦帮之山火贲) 的大作中提到: 】 : 大悲剧 : 算法一窍不通
fykhlp机器人#7 · 2011/5/22
自己在纸上模拟一下就懂了