#include <bits/stdc++.h>
#define int long long
#define file(task) if(fopen(task".inp", "r")) { freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); }
#define FOR(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for(int i = (a), _b = (b); i >= _b; --i)
#define all(X) (X).begin(), (X).end()
#define MASK(X) (1ll << (X))
#define bit(X, i) (((X)>>(i))&1)
#define SZ(X) (int)(X).size()
using namespace std;
const int maxN = 1e6;
struct Trie
{
int ch[26];
int exist;
Trie() { memset(ch, -1, sizeof(ch)); exist = 0; }
} Tr[maxN+5];
int N;
string s[maxN+5];
void read_input()
{
cin >> N;
FOR(i, 1, N) cin >> s[i];
}
namespace greedy
{
int cnt = 1;
void add(string s)
{
int p = 1;
for(char c : s)
{
int x = c - '0';
if(Tr[p].ch[x] == -1) Tr[p].ch[x] = ++cnt;
p = Tr[p].ch[x];
}
Tr[p].exist = true;
}
void solve()
{
FOR(i, 1, N)
{
sort(all(s[i]));
add(s[i]);
}
cout << cnt;
}
}
namespace sub
{
const int Nsub = 18;
const int maxNsub = MASK(18);
const int inf = 1e9;
int f[maxNsub+5], cnt[Nsub+5][30];
int mn[maxNsub+5];
void solve()
{
memset(f, 0x3f3f3f, sizeof(f));
FOR(i, 1, N)
{
for(char c : s[i]) ++cnt[i][c - 'a'];
f[MASK(i - 1)] = SZ(s[i]);
}
FOR(mask, 1, MASK(N) - 1)
{
FOR(c, 0, 25)
{
int cur = inf;
FOR(i, 1, N)
{
if(bit(mask, i - 1)) cur = min(cur, cnt[i][c]);
}
mn[mask] += cur;
}
}
FOR(x, 0, MASK(N) - 1)
{
int s = (MASK(N) - 1) ^ x;
for(int y = s; y; y = (y - 1) & s)
{
f[x|y] = min(f[x|y], f[x] + f[y] - mn[x|y]);
}
}
cout << f[MASK(N) - 1] + 1;
}
}
void solve()
{
sub::solve();
}
int32_t main()
{
cin.tie(0)->sync_with_stdio(0);
file("TRIE");
read_input();
solve();
return 0;
}
Compilation message
vjestica.cpp: In function 'int32_t main()':
vjestica.cpp:5:56: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
5 | #define file(task) if(fopen(task".inp", "r")) { freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
vjestica.cpp:116:5: note: in expansion of macro 'file'
116 | file("TRIE");
| ^~~~
vjestica.cpp:5:89: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
5 | #define file(task) if(fopen(task".inp", "r")) { freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
vjestica.cpp:116:5: note: in expansion of macro 'file'
116 | file("TRIE");
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
15 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Runtime error |
26 ms |
65536 KB |
Execution killed with signal 9 |
3 |
Runtime error |
15 ms |
65536 KB |
Execution killed with signal 9 |
4 |
Runtime error |
14 ms |
65536 KB |
Execution killed with signal 9 |
5 |
Runtime error |
11 ms |
65536 KB |
Execution killed with signal 9 |
6 |
Runtime error |
13 ms |
65536 KB |
Execution killed with signal 9 |
7 |
Runtime error |
12 ms |
65536 KB |
Execution killed with signal 9 |
8 |
Runtime error |
13 ms |
65536 KB |
Execution killed with signal 9 |
9 |
Runtime error |
11 ms |
65536 KB |
Execution killed with signal 9 |
10 |
Runtime error |
24 ms |
65536 KB |
Execution killed with signal 9 |