注意:
1.读入带空格的字符串scanf("%[^\n]")
2.while循环要设置好循环终止条件,否则会死循环或者数组下标访问溢出
#pragma warning(disable:4996)
#include<iostream>
#include<string>
#include<cstdio>
#include<cmath>
using namespace std;
int strlen(string a)
{
int n = 0;
while (a[n] != '\0')
n++;
return n;
}
void reverse(std::string& a)
{
int len = strlen(a);
for (int i = 0; i < len / 2; i++)
{
char temp;
temp = a[i];
a[i] = a[len - 1 - i];
a[len - 1 - i] = temp;
}
}
int main()
{
int n;
string str[110];
scanf("%d", &n);
int count = 260;
for (int i = 0; i < n; i++)
{
string temp;
temp.resize(260, '\0');
getchar();
scanf("%[^\n]", &temp[0]);
reverse(temp);
str[i] = temp;
if (i > 0)
{
int num = 0;
int len1 = strlen(str[i]), len2 = strlen(str[i - 1]);
int len = len1 > len2 ? len2 : len1;
while (str[i][num] == str[i - 1][num] && num < len)
{
num++;
}
if (num < count)
{
count = num;
if (count == 0)
{
printf("nai");
return 0;
}
}
}
}
int flag = 1;
for (int i = 1; i < n&&flag == 1; i++)
{
for (int j = 0; j < count; j++)
{
if (str[i][j] != str[0][j])
{
flag = 0;
break;
}
}
}
if (flag == 0)
{
printf("nai");
}
else
{
int i = count - 1;
if (str[0][count - 1] == ' ')
i--;
while (i >= 0)
{
printf("%c", str[0][i]);
i--;
}
}
return 0;
}