java獲取兩個字符串中最大相同子串。第一個動作:將短的那個串進行長度一次遞減的子串打印

package day13;
/*
獲取兩個字符串中最大相同子串。第一個動作:將短的那個串進行長度一次遞減的子串打印。
	"abcwerthelloyuiodef"
	"cvhellobnm"
	思路:
		1,將短的那個子串按照長度遞減的方式獲取到。
		2,將每獲取到的子串去長串中判斷是否包含,
			如果包含,已經找到!。
 */
public class StringDemo9 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String s1 = "abcwerthelloyuiodef";
		String s2 = "cvhellobnm";
		sop(getMaxSubString(s2,s1));
	}
	public static String getMaxSubString(String s1,String s2)
	{
		String max = "",min = "";
		max = (s1.length()>s2.length())?s1: s2;
		min = (max==s1)?s2: s1;
		for(int x=0; x<min.length(); x++)
		{
			for(int y=0,z=min.length()-x; z!=min.length()+1; y++,z++)
			{
				String temp = min.substring(y,z);
				sop(temp);
				if(max.contains(temp))//if(s1.indexOf(temp)!=-1)
					return temp;
			}
		}
		return "";
	}
	public static void sop(String str)
	{
		System.out.println(str);
	}

}

發佈了51 篇原創文章 · 獲贊 1 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章