Submission #785924

#TimeUsernameProblemLanguageResultExecution timeMemory
785924MilosMilutinovicRima (COCI17_rima)C++14
42 / 140
1067 ms262144 KiB
/** * author: wxhtzdy * created: 17.07.2023 21:06:38 **/ #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<string> s(n); for (int i = 0; i < n; i++) { cin >> s[i]; } vector<vector<bool>> f(n, vector<bool>(n)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int si = (int) s[i].size(); int sj = (int) s[j].size(); int ptr = 0; while (ptr < min(si, sj) && s[i][si - ptr - 1] == s[j][sj - ptr - 1]) { ptr += 1; } f[i][j] = f[j][i] = (ptr >= max(si, sj) - 1); } } vector<vector<bool>> dp(1 << n, vector<bool>(n)); for (int i = 0; i < n; i++) { dp[1 << i][i] = true; } for (int mask = 1; mask < (1 << n); mask++) { for (int i = 0; i < n; i++) { if (!dp[mask][i]) { continue; } for (int j = 0; j < n; j++) { if (mask >> j & 1) { continue; } if (f[i][j]) { dp[mask ^ (1 << j)][j] = true; } } } } int ans = 0; for (int mask = 0; mask < (1 << n); mask++) { for (int i = 0; i < n; i++) { if (dp[mask][i]) { ans = max(ans, __builtin_popcount(mask)); } } } cout << ans << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...