-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathA1077.cpp
55 lines (52 loc) · 1.2 KB
/
A1077.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* hint:
* 1. <algorithm>:reverse()
* 2. PAT 不再支持 <cstdio>:gets(),可以用cin.getline() 来替代。此两个函数都是遇到 '\n'停止读取
*/
#include <iostream>
#include <cstring>
// #include <cstdio>
#include <algorithm>
#define MAXN 1005
#define MAXSIZE 260
using namespace std;
char words[MAXN][MAXSIZE];
int main()
{
int n, minLen = MAXSIZE;
cin >> n;
getchar(); // get rid of the '\n'
for (int i = 0; i < n; i++)
{
// gets(words[i]); // PAT not supports gets()
cin.getline(words[i], MAXSIZE);
int len = strlen(words[i]);
if (len <= minLen) minLen = len;
reverse(words[i], words[i] + len);
}
int cnt = 0;
for (int i = 0; i < minLen; i++)
{
bool consistent = true;
char ch = words[0][i];
for (int j = 1; j < n; j++)
{
if (ch != words[j][i])
{
consistent = false;
break;
}
}
if (consistent) cnt++;
else break;
}
if (cnt)
{
for (int i = cnt - 1; i >= 0; i--)
printf("%c", words[0][i]);
printf("\n");
}
else
printf("nai\n");
return 0;
}