제출 #1000464

#제출 시각아이디문제언어결과실행 시간메모리
1000464MilosMilutinovicRima (COCI17_rima)C++14
0 / 140
1041 ms262144 KiB
#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]; } for (int i = 0; i < n; i++) { reverse(s[i].begin(), s[i].end()); } auto Good = [&](string s, string t) { if (s.size() > t.size()) { swap(s, t); } int p = 0; while (p < (int) s.size() && s[p] == t[p]) { p += 1; } return p >= (int) t.size() - 1; }; vector<vector<int>> dp(1 << n, vector<int>(n)); for (int mask = 1; mask < (1 << n); mask++) { for (int i = 0; i < n; i++) { if (!(mask >> i & 1)) { continue; } int mx = 1; for (int j = 0; j < n; j++) { if (!(mask >> j & 1)) { continue; } if (Good(s[i], s[j])) { mx = max(mx, dp[mask ^ (1 << i)][j] + 1); } else { mx = max(mx, dp[mask ^ (1 << i)][j]); } } dp[mask][i] = mx; } } int res = 0; for (int mask = 0; mask < (1 << n); mask++) { for (int i = 0; i < n; i++) { res = max(res, dp[mask][i]); } } cout << res << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...