返回信息流求两个字符串的公共最大子串.......
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));;
}
}
这是一条镜像帖。来源:北邮人论坛 / java / #18453同步于 2011/5/20
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
求大牛讲解一下这个程序
nuckid
2011/5/20镜像同步7 回复
订阅后,新回复会通过你的通知中心匿名送达。
7 条回复
大悲剧
算法一窍不通
【 在 nuckid (Kid Rock) 的大作中提到: 】
: 求两个字符串的公共最大子串.......
: public class getLCStringTest {
: public static String getLCSring(String str1,String str2){
: ...................