思路:
暴力枚举。
实现:
1 #include <iostream> 2 #include <set> 3 #include <vector> 4 using namespace std; 5 6 void get_subseq(string s, string x, int cur, set<string>& st) 7 { 8 if (cur == s.length()) { if (x != "") st.insert(x); return; } 9 get_subseq(s, x + s[cur], cur + 1, st); 10 get_subseq(s, x, cur + 1, st); 11 } 12 13 bool is_subseq(string s, string t) 14 { 15 int i = 0, j = 0; 16 while (i < s.length() && j < t.length()) 17 { 18 if (t[j] != s[i]) j++; 19 else i++; 20 } 21 return i == s.length(); 22 } 23 24 bool check(string s, vector<string> v) 25 { 26 for (int i = 1; i < v.size(); i++) 27 { 28 bool flg = false; 29 int l = v[i].length(); 30 for (int j = 0; j < l; j++) 31 { 32 string tmp = v[i].substr(j, l - j) + v[i].substr(0, j); 33 if (is_subseq(s, tmp)) { flg = true; break; } 34 } 35 if (!flg) return false; 36 } 37 return true; 38 } 39 40 int main() 41 { 42 int n; 43 string s; 44 while (cin >> n) 45 { 46 vector<string> v; 47 for (int i = 0; i < n; i++) { cin >> s; v.push_back(s); } 48 set<string> st; 49 vector<string> buf; 50 get_subseq(v[0], "", 0, st); 51 for (auto it: st) 52 { 53 int l = it.length(); 54 for (int i = 1; i < l; i++) 55 { 56 string tmp = it.substr(i, l - i) + it.substr(0, i); 57 buf.push_back(tmp); 58 } 59 } 60 for (auto it: buf) st.insert(it); 61 int maxn = 0; string ans = "zzzzzzzzzz"; 62 for (auto it: st) 63 { 64 if (check(it, v) && (it.length() > maxn || (it.length() == maxn && it < ans))) 65 { 66 ans = it; 67 maxn = it.length(); 68 } 69 } 70 if (ans == "zzzzzzzzzz") cout << 0 << endl; 71 else cout << ans << endl; 72 } 73 return 0; 74 }