思路:1、对于输入的每一组字符串,对字符串进行反转
2、比较该组中第一个字符串的所有字符和剩余字符串的字符匹配个数,如果第i个字符和剩余字符串都匹配成功,则加入结果字符串中。不匹配则直接终止输出。
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (!sc.hasNext("0")) {
int x = Integer.parseInt(sc.next());
String[] s = new String[x];
for (int i = 0; i < x; i ++) {
s[i] = sc.next();
s[i] = reverse(s[i]);
}
String res = "";
for (int i = 0; i < s[0].length(); i ++) {
boolean state = true;
for (int j = 1; j < x; j ++) {
if (i >= s[j].length()) {
state = false;
break;
}
if (s[j].charAt(i) != s[0].charAt(i) ) {
state = false;
break;
}
}
if (state) {
res += s[0].charAt(i);
} else {
break;
}
}
System.out.println(reverse(res));
}
}
public static String reverse(String s) {
int i = 0, j = s.length() - 1;
char[] ch = s.toCharArray();
while (i < j) {
char temp = ch[i];
ch[i] = ch[j];
ch[j] = temp;
i ++;
j --;
}
return new String(ch);
}
}