1077 Kuchiguse

重点在于For each test case, print in one line the kuchiguse of the character, i.e., the longest common suffix of all N lines. If there is no such suffix, write nai.
求给出字符串的公共后缀。

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
#include <bits/stdc++.h>
using namespace std;

int main() {
int n;
string result;
scanf("%d\n", &n);
for (int i = 0; i < n; i++) {
string s;
getline(cin, s);
int len = s.length();
reverse(s.begin(), s.end());
if (i == 0) {
result = s;
}
else {
int lenResult = result.length();
int minlen = min(len, lenResult);
for (int j = 0; j < minlen; j++) {
if (result[j] != s[j]) {
result = s.substr(0, j);
break;
}
}
}
}
reverse(result.begin(), result.end());
if (result.length() == 0) {
cout << "nai";
}
else {
cout << result;
}
cout << endl;

return 0;
}

// scanf“%d”少了个\n出错

3
Itai nyan~
Ninjin wa iyadanyan~
uhhh nyan~

nyan~