簡單題,用C++寫了...
需要注意的是,cin了之後不會把 吃掉,需要再getchar一次才能夠吃掉 ,然後接着geline才正確
感覺沒什麼美感...
1 #include <vector> 2 #include <iostream> 3 #include <string> 4 #include <algorithm> 5 #include <limits> 6 #include <cstdio> 7 8 using namespace std; 9 10 int main(){ 11 int N; 12 cin >> N; 13 getchar(); 14 15 vector<string> lines; 16 int shortest = 0x7fffffff; 17 for (int i = 0; i < N; i++){ 18 string str; 19 getline(cin, str); 20 21 if (str.size() < shortest) 22 shortest = str.size(); 23 24 lines.push_back(str); 25 } 26 27 int common_len = 0; 28 for (int i = 0; i < shortest; i++){ 29 char ch = lines[0][lines[0].size() - i - 1]; 30 31 int cnt = 1; 32 for (int j = 1; j < lines.size(); j++){ 33 string cur_str = lines[j]; 34 if (cur_str[cur_str.size() - i - 1] != ch) 35 break; 36 cnt++; 37 } 38 39 if (cnt == lines.size()) 40 common_len++; 41 else 42 break; 43 } 44 45 if (common_len == 0) 46 cout << "nai" << endl; 47 else{ 48 string res = lines[0].substr(lines[0].size() - common_len, lines[0].size()); 49 cout << res << endl; 50 } 51 52 return 0; 53 }