Submission #96722

#TimeUsernameProblemLanguageResultExecution timeMemory
96722luckyboyVještica (COCI16_vjestica)C++14
160 / 160
315 ms1268 KiB
/**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; }
#Verdict Execution timeMemoryGrader output
Fetching results...