❶ java怎麼寫求最長的公共子序列
/*目標:輸出兩個字元串的所有公共最長子序列date:09-11-26BY:zggxjxcgx演算法:判斷較短串是否為較長串的子序列,如果是則得到結果;否則,對較短串進行逐個字元刪除操作(將字元替換為'#'表示刪除)。刪除操作用遞歸函數進行實現。每層遞歸刪除一個字元,若刪除一個字元後未得到匹配子序列,則還原該字元所在位置。第n層遞歸未找到匹配子序列,則將遞歸層數加1,重復刪除直到剩下空串。*/#include#includeintdep=0;/*較短串的長度*/intdepflag=0;/*下一步要探測的深度*/intlastflag=0;/*是否找到匹配子序列,1為找到*/intcount=0;/*目標結果的個數*/intmystrcmp(char*s1,char*s2)/*判斷s2是否為s1的子串*/{while(*s1*s2)if(*s2=='#')s2++;elseif(*s2==*s1){s1++;s2++;}elses1++;while(*s2=='#')s2++;if(*s2=='\0')return1;return0;}voidpristr(char*str)/*列印最長子序列*/{if(0==count++)printf("\n公共最長子序列:\n");printf("%2d:",count);while(*str){if(*str!='#')printf("%c",*str);str++;}printf("\n");}/*遞歸函數求最長子序列。start控制下一個要檢測的字元,deptemp控制遞歸的深度,first為's'表示第一層遞歸*/intfun(char*str1,char*str2,char*start,intdeptemp,charfirst){inti=0,j=0;char*s,cback;do{s=start;if('s'==first)deptemp=depflag;/*在第一層設置遞歸深度*/while(*s){if(*s=='#'){s++;continue;}cback=*s;*s='#';/*刪除當前字元*/if(mystrcmp(str1,str2)){pristr(str2);lastflag=1;}/*找到匹配,將lastflag設為1,在完成深度為deptemp+1的探測(找到所有匹配)後退出遞歸*/elseif(deptemp>0)fun(str1,str2,s+1,deptemp-1,'n');/*深度探測,s+1表示從下一個位置開始刪除*/*s=cback;s++;/*還原該位置的字元,以便下次進行探測*/}if('s'==first)++depflag;/*刪除depflag+1個字元還未找到,則遞歸深度加1*/}while(depflagstrlen(st2))s1=st1,s2=st2;/*將s1設為較長的串*/elses1=st2,s2=st1;printf("\nstr1:%s\nstr2:%s\n",s1,s2);dep=strlen(s2);/*得到較短串的長度*/if(mystrcmp(s1,s2))pristr(s2);elseif(0==fun(s1,s2,s2,0,'s'))printf("\n沒有公共元素!\n");//printf("%d\n",mystrcmp("afdebjewcwedw","abcdw#"));}
❷ 求兩個字元串的最長公共子串,要求輸入兩個字元串,輸出他們的最長公共子串,包括長度。
遍歷一下就好了,java代碼:
public class CommonSubString {
public String search(String s1,String s2)
{
String max = "";
for(int i=0; i<s1.length(); i++)
{
for(int j=i; j<s1.length(); j++)
{
String sub = s1.substring(i,j);
if((s2.indexOf(sub)!=-1)&&sub.length()>max.length())
{
max = sub;
}
}
}
return max;
}
public static void main(String[] args)
{
String s1 = "abcdefghigj";
String s2 = "xyzabcdefigj";
String output = new CommonSubString().search(s1,s2);
System.out.println(output);
}
}
❸ java求最大公共子串
二樓改的
c = b.substring(i,j-1);
之後下標越界沒了。
程序無法出結果,中間有死循環。當while語句執行完之後j是a.length();然後是執行內循環for語句for(j=b.length()-1;j>0;j--) 此時只比較J>0;。。。。好像是個死循環。最內層的循環可以在加一個int k來控制。多次運行導致cpu上升至100%的。
提供一種矩陣演算法,這是LCS的一種演算法:
public class stringCompare {
/**求最長匹配子字元串演算法
str數組記錄每行生成的最大值strmax
max數組記錄str數組最大時所處的列號nummaxj
最大子字元串為substring(nummax-strmax+1,strmax+1)
*/
public static void main(String[] args) {
String s1="asdfgxxcvasdfgc";
String s2="asdfxxcv";
int m=s1.length();
int n=s2.length();
int k=0;
int nummax=0;
int []str=new int[m];
int []max=new int[m];
int []num=new int[m];
for(int i=0;i<m;i++) //生成矩陣數組
for(int j=n-1;j>=0;j--)
{
if(s1.charAt(i)==s2.charAt(j))
if(i==0||j==0)
{
num[j]=1;
max[i]=j;
str[i]=1;
}
else
{
num[j]=num[j-1]+1;
if(max[i]<num[j])
{
max[i]=j;
str[i]=num[j];
}
}
else
num[j]=0;
}
for(k=0;k<m;k++) //求str數組的最大值
{
if(nummax<str[k])
{
nummax=str[k];
}
}
for(k=0;k<m;k++)
if(nummax==str[k])
System.out.println(s2.substring(max[k]-str[k]+1,max[k]+1));
}
}
❹ java 兩個字元串,求最長公共子串的長度
用動態規劃,
if (s1[i] == s2[j])
dp[i][j] = dp[i - 1][j - 1] + 1;
else
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
❺ JAVA超長字元串問題
2種方案
1)字元串分段處理。當然這裡面也有問題,如果abcd,bc被分到2個字元串裡面,拿取也聽不方便的。不過也不失為一個辦法。
2)對於特定的內容,如果可以轉化為xml,可以轉化成xml,然後層層解析;取得所需要的內容。
覺得有點恐怖,怎麼那麼長的字元串拼接。通過tochararay那些都行不通的。數組承受不了那麼多的元素。因為數組裡面定義也是object[int len]的
❻ java語言中編程求解兩個字元串最長相同字元串的長度
public class StringTest4 {
/**
* @param args
*/
public static void main(String[] args) {
/*
* 需求4:兩個字元串的最大相同子串。
* "sadfcctvghjkl"
* "zxcctvcv"
*
* 思路:
* 1,以短的字元串為主。
* 到長的字元串中去判斷是否存在,如果存在,已找到。
* 2,如果沒有找到。將短的字元串的長度遞減獲取子串繼續到長的串中查找。只要找到就結束。
* 3,沒有找到,說明沒有相同的。
*
*/
String s1 = "sadfcctvghjkl";
String s2 = "zxcctvcv";
String maxSub = getMaxSubString(s2,s1);
System.out.println("maxsub="+maxSub+" length="+maxSub.length());
}
public static String getMaxSubString(String s1, String s2) {
//確定哪個是長的哪個是短的。
String longStr,shortStr;
longStr = s1.length()>s2.length()?s1:s2;
shortStr = s1.equals(longStr)?s2:s1;
// System.out.println("long:"+longStr);
// System.out.println("short:"+shortStr);
//對短的字元串操作,從短串中取子串,到長字元串中判斷,是否存在。
for(int x=0; x<shortStr.length(); x++){
for(int y=0,z=shortStr.length()-x; z<=shortStr.length(); y++,z++){
//根據y,z,獲取子串。
String temp = shortStr.substring(y,z);
// System.out.println(temp);
if(longStr.contains(temp))
return temp;
}
}
return null;
}
}