AcWing 779. 最长公共字符串后缀
原题链接
简单
作者:
123666
,
2024-11-15 19:35:26
,
所有人可见
,
阅读 2
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(true){
int a =sc.nextInt();
if(a==0){
break;
}
if(a==1){
System.out.println(sc.next());
continue;
}
String str[] = new String[a];
for(int i = 0;i<a;i++){
str[i]=sc.next();
//System.out.println(str[i]);
}
int temp = 0;
for(int j =1;j<str[0].length()+1;j++ ){
String out = str[0].substring(str[0].length()-j);
for(int i =1;i<a;i++){
if(str[i].length()<j){
//System.out.println("情况1");
out=str[i];
temp = 1;
break;
}
//System.out.println("out: "+out+" 比较" + str[i].substring(str[i].length()-j));
if(!out.equals(str[i].substring(str[i].length()-j))){
if(j==1){
//System.out.println("情况2");
out="";
}
else{
//System.out.println("情况3");
out=str[i].substring(str[i].length()-j+1);
}
temp = 1;
break;
}
}
if(temp==1){
System.out.println(out);
break;
}
if(temp==0 && j==str[0].length()){
System.out.println(str[0]);
break;
}
}
}
}
}