AcWing 779. 最长公共字符串后缀 - Java
原题链接
困难
作者:
KYCygni
,
2021-03-20 10:16:21
,
所有人可见
,
阅读 294
Java 代码
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
while ( n > 0)
{
String[] s = new String[n];
int min_len = 200;
for (int i = 0; i < n; i ++)
{
s[i] = cin.next();
if (min_len > s[i].length())
min_len = s[i].length();
}
boolean is_success = true;
for (int j = 0; j < min_len; j ++)
{
char c = s[0].charAt(s[0].length() - 1 - j);
for (int k = 1; k < s.length; k ++)
{
if (c != s[k].charAt(s[k].length() - 1 - j))
{
is_success = false;
break;
}
}
if (!is_success)
{
System.out.println (s[0].substring(s[0].length() - j));
break;
}
}
if (is_success)
System.out.println (s[0].substring(s[0].length() - min_len));
n = cin.nextInt();
}
}
}