/**Lucky Boy**/
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define maxc 1000000007
#define maxn 20
#define maxm 1000006
#define pii pair <short,short>
#define Task ""
template <typename T> inline void read(T &x){char c;bool nega=0;while((!isdigit(c=getchar()))&&(c!='-'));if(c=='-'){nega=1;c=getchar();}x=c-48;while(isdigit(c=getchar())) x=x*10+c-48;if(nega) x=-x;}
template <typename T> inline void writep(T x){if(x>9) writep(x/10);putchar(x%10+48);}
template <typename T> inline void write(T x){if(x<0){putchar('-');x=-x;}writep(x);putchar(' ');}
template <typename T> inline void writeln(T x){write(x);putchar('\n');}
using namespace std;
int n,cnt[maxn][26],dp[1 << maxn],qwe[26];
int Calc(int state)
{
FOR(i,0,25) qwe[i] = maxc;
FOR(i,0,n-1)
if ((state >> i) & 1)
{
FOR(j,0,25)
qwe[j] = min(qwe[j],cnt[i+1][j]);
}
int res = 0;
FOR(i,0,25) res += qwe[i];
return res;
}
int Dp(int state)
{
int &cur = dp[state];
if (cur != maxc) return cur;
int sub = Calc(state);
if ((state & -state) == state) return cur = sub;
for (int i = state; i > 0; i = (i - 1) & state)
{
if (i == state) continue;
cur = min(cur,Dp(i) + Dp(state ^ i) - sub);
}
return cur;
}
int main()
{
ios_base::sync_with_stdio(NULL);cin.tie(NULL);cout.tie(NULL);
//freopen(Task".inp", "r",stdin);
//freopens(Task".out", "w",stdout);
cin >> n;
FOR(i,1,n)
{
string s;
cin >> s;
for (char c : s) ++cnt[i][c - 'a'];
}
FOR(i,0,(1 << n) - 1) dp[i] = maxc;
cout << Dp((1 << n) - 1) + 1;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
305 ms |
760 KB |
Output is correct |
5 |
Correct |
315 ms |
764 KB |
Output is correct |
6 |
Correct |
313 ms |
1016 KB |
Output is correct |
7 |
Correct |
305 ms |
1268 KB |
Output is correct |
8 |
Correct |
314 ms |
1144 KB |
Output is correct |
9 |
Correct |
300 ms |
1212 KB |
Output is correct |
10 |
Correct |
314 ms |
1144 KB |
Output is correct |