# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
244878 | NONAME | Rima (COCI17_rima) | C++14 | 834 ms | 36572 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define dbg(x) cerr << #x << " = " << x << "\n"
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0); cout.tie()
using namespace std;
using ll = long long;
const int N = 5e5 + 10;
int n, f[(1 << 20)][20];
string s[N];
bool gd(int x, int y) {
int l1 = int(s[x].size()), l2 = int(s[y].size()), k = 0;
if (max(l1, l2) - min(l1, l2) > 1)
return 0;
while (k < min(l1, l2) && s[x][k] == s[y][k])
++k;
return (k >= max(l1, l2) - 1);
}
int main() {
fast_io;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> s[i];
reverse(s[i].begin(), s[i].end());
}
if (n > 20)
return void(cout << "0\n"), 0;
for (int i = 0; i < (1 << n); ++i)
for (int j = 0; j < n; ++j)
f[i][j] = 0;
for (int i = 0; i < n; ++i)
f[(1 << i)][i] = 1;
for (int msk = 1; msk < (1 << n); ++msk)
for (int i = 0; i < n; ++i) {
if (!(msk & (1 << i)))
continue;
for (int j = 0; j < n; ++j)
if (!(msk & (1 << j)) && gd(i, j))
f[msk | (1 << j)][j] = max(f[msk | (1 << j)][j], f[msk][i] + 1);
}
int ans = 0;
for (int i = 0; i < (1 << n); ++i)
for (int j = 0; j < n; ++j)
ans = max(ans, f[i][j]);
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |