算法实现-->最长公共子串

 /*
 * 最长公共子串
 * Longest Common SubString
 * 
 */
public class LCSS {

	
	String str1,str2;
	
	String sub;
	
	LCSS(String str1,String str2){
		 
		this.str1=str1;
		this.str2=str2;
		
	}
	public static void main(String []args){
		
		String str1="BDCABABCBDA";
		String str2="ABCBDAB";
	 
		LCSS lcss=new LCSS(str1,str2);
		
	//	lcss.methodOne();
		
	  lcss.methodThree();
	}
	//字符串匹配算法
	public boolean isMatch(String str,String sub){
		   
		     
		   int i=0,j=0;
		   
		   while(imaxlong){
					
					maxlong =length;

				}
				
				
				
			}
			
		}
		 
		
		
	}
	//暴力求解法1
	public void methodOne(){
		
		
		int subLong=0;//子串的长度
		int maxLong=0;//最大的长度
		int flagi=-1,flagj=-1;
		for(int i=0;i"+str1.substring(i,i+j+1));
					subLong=subLong+1;
					flag=j;
					
				} 
				
			}	
			if(subLong>maxLong){
				maxLong=subLong;
				flagi=i;
				flagj=flag;
			
			}

		}
		
		System.out.println("Res:"+flagi+"--------"+flagj+"  ");
		
		
	}
	
	//动态规划求解法 
	public void methodThree(){
		
		
		int [][]c=new int[str1.length()][str2.length()];
		
		
		int flag=0;
		for(int i=0;imax){
					flagi=i;
					flagj=j;
					max=c[i][j];
					
				}
				
				
			}
			
		}
		
		System.out.println(" ------->"+str1.substring(flagi-max+1, flagi+1));
		 
		
		
		
		
	}
	
	
}

你可能感兴趣的:(算法实现)