一、题目 二、代码 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { String str1 = in.nextLine(); String str2 = in.nextLine(); String maxStr = str1.length() >= str2.length()? str1:str2; String minStr = str1.length() < str2.length()? str1:str2; if (maxStr.contains(minStr)) { System.out.println(minStr); } int max = 0, begin = 0; for (int i = 0; i < minStr.length()-1; i++) { for (int j = i+1; j <= minStr.length(); j++) { String temp = minStr.substring(i,j); if (!maxStr.contains(temp)) { continue; } if (max < j-i) { begin = i; max = j-i; } } } System.out.println(minStr.substring(begin,begin+max)); } } }