关键点:
1.反转
2.竖着判断,慢慢增加最长前缀的长度
输入样例
3
Itai nyan~
Ninjin wa iyadanyan~
uhhh nyan~
输出样例
nyan~
C++代码
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int N = 310;
int n, minlen = 256, ans = 0;
char s[100][256];
int main( ){
scanf("%d", &n);
getchar();
for ( int i=0; i < n; i++ ) {
cin.getline(s[i], N);
int len = strlen(s[i]);
if ( len < minlen ) minlen = len;
for ( int j=0; j < len / 2; j++ ) {
char tmp = s[i][j];
s[i][j] = s[i][len - j -1];
s[i][len -j - 1] = tmp;
}
}
for ( int i=0; i < minlen; i++ ) {
char c = s[0][i];
bool same = true;
for ( int j=1; j <n; j++ ) {
if ( c != s[j][i] ) {
same = false;
break;
}
}
if( same ) ans++;
else break;
}
if ( ans ) {
for ( int i = ans-1; i>=0; i-- ) {
printf("%c", s[0][i]);
}
}
else {
printf("nai");
}
return 0;
}