题目描述 给你四个字符串,你需要求出包含这四个字符串作为子串的最短字符串长度 #include <bits/stdc++.h> using namespace std; typedef long long ll;
string s[4];
int a[24][4] = { {0, 1, 2, 3}, {0, 1, 3, 2}, {0, 2, 1, 3}, {0, 2, 3, 1}, {0, 3, 1, 2}, {0, 3, 2, 1}, {1, 0, 2, 3}, {1, 0, 3, 2}, {1, 2, 0, 3}, {1, 2, 3, 0}, {1, 3, 0, 2}, {1, 3, 2, 0}, {2, 0, 1, 3}, {2, 0, 3, 1}, {2, 1, 0, 3}, {2, 1, 3, 0}, {2, 3, 0, 1}, {2, 3, 1, 0}, {3, 0, 1, 2}, {3, 0, 2, 1}, {3, 1, 0, 2}, {3, 1, 2, 0}, {3, 2, 0, 1}, {3, 2, 1, 0}, };
int unite() { int ans = 40; for (int i = 0; i < 24; ++i) { string t = s[a[i][0]]; for (int j = 1; j < 4; ++j) { string nex = s[a[i][j]]; //cout << t << ' ' << nex << endl; if (t.find(nex) != string::npos) continue; else { int pos = 0; int len = min((int)t.size(), (int)nex.size()); for (int k = len; k >= 1; --k) { string temp = nex.substr(0, k); if (t.rfind(temp) == t.size() - k) { pos = k; break; } } t += nex.substr(pos); } } ans = min(ans, (int)t.size()); } return ans; }
signed main() { ios::sync_with_stdio(false), cin.tie(0); for (int i = 0; i < 4; ++i) cin >> s[i]; cout << unite() << endl; system("pause"); return 0; }