# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
100188 | pamaj | Rima (COCI17_rima) | C++14 | 77 ms | 20176 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int maxn = 19;
#define int long long
int dp[1 << maxn], n;
vector<string> s;
bool rhyme(const string& a, const string& b)
{
int u = (int)a.size() - 1, v = (int)b.size() - 1;
int cont = 0;
while(a[u] == b[v] and u >= 0 and v >= 0)
{
cont++, u--, v--;
}
if(cont >= max((int)a.size(), (int)b.size()) - 1) return true;
return false;
}
int solve(int mask)
{
if(dp[mask] != -1) return dp[mask];
int ff = -1;
bool ok = true;
for(int i = 0; i < n; i++)
{
if(mask & (1 << i))
{
if(ff == -1)
ff = i;
else
{
if(!rhyme(s[i], s[ff]))
{
ok = false;
break;
}
ff = i;
}
}
}
if(!ok) return 0;
if(mask == (1 << n) - 1) return n;
int ans = __builtin_popcount(mask);
for(int i = 0; i < n; i++)
{
if(mask & (1 << i)) continue;
ans = max(ans, solve(mask | (1 << i)));
}
//string a;
// int aux = mask;
// while(aux)
// {
// a += aux % 2 + '0';
// aux /= 2;
// }
// reverse(a.begin(), a.end());
//cout << a << "\n";
return dp[mask] = ans;
}
int32_t main()
{
ios::sync_with_stdio(false), cin.tie(nullptr);
cin >> n;
for(int i = 0; i < n; i++)
{
string a;
cin >> a;
s.push_back(a);
}
memset(dp, -1, sizeof(dp));
cout << solve(0) << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |