当前位置:网站首页>[COCI] Vje š TICA (subset DP)
[COCI] Vje š TICA (subset DP)
2022-04-23 09:43:00 【Zimba_】
The question :
Given n ( n ≤ 16 ) n(n\leq 16) n(n≤16) A string . You can arrange the letters in the string at will , So that the number of nodes after inserting these strings into the dictionary tree is the least .
The total length of the string shall not exceed 1 0 6 10^{6} 106.
Ideas :
Only 16 16 16 position , Consider the state pressure representation set .
use D P ( ( 10111 ) 2 ) DP((10111)_{2}) DP((10111)2) It means the first one 1 , 3 , 4 , 5 1,3,4,5 1,3,4,5 The minimum number of knots after the string is inserted .
First consider inserting two strings into the dictionary tree , The number of nodes contributed is the sum of the number of nodes of two strings minus the prefix of two strings .
therefore , We consider that the prefix of two strings should be the longest , Is the number of each letter m i n min min And .
Then consider a few strings " a a b c " , " a a b b " . " a b c " "aabc","aabb"."abc" "aabc","aabb"."abc". Can greedily take out " a b " "ab" "ab" Put it in the front , Then left " a c " , " a b " . " c " "ac","ab"."c" "ac","ab"."c" Think again .
use P R E ( S ) PRE(S) PRE(S) Represents a collection S S S The longest common prefix of all strings in , Consider from ( " a c " , " a b " ) ("ac","ab") ("ac","ab") and ( " c " ) ("c") ("c") Merge into ( " a c " , " a b " , " c " ) ("ac","ab","c") ("ac","ab","c").
D P ( " a a b c " , " a a b b " , " a b c " ) DP("aabc","aabb","abc") DP("aabc","aabb","abc")
= D P ( " a c " , " a b " , " c " ) + P R E ( “ a a b c ” , " a a b b " . " a b c " ) =DP("ac","ab","c")+PRE(“aabc”,"aabb"."abc") =DP("ac","ab","c")+PRE(“aabc”,"aabb"."abc")
= D P ( " a c " , " a b " ) + D P ( " c " ) + P R E ( " a a b c " , “ a a b b ” , " a b c " ) =DP("ac","ab")+DP("c")+PRE("aabc",“aabb”,"abc") =DP("ac","ab")+DP("c")+PRE("aabc",“aabb”,"abc")
= D P ( " a a b c " , " a a b b " ) − P R E ( " a b " , " a b " ) + D P ( " a b c " ) − P R E ( " a b " ) + P R E ( " a a b c " , “ a a b b ” , " a b c " ) =DP("aabc","aabb")-PRE("ab","ab")+DP("abc")-PRE("ab")+PRE("aabc",“aabb”,"abc") =DP("aabc","aabb")−PRE("ab","ab")+DP("abc")−PRE("ab")+PRE("aabc",“aabb”,"abc")
= D P ( " a a b c " , " a a b b " ) + D P ( " a b c " ) − P R E ( " a a b c " , “ a a b b ” , " a b c " ) =DP("aabc","aabb")+DP("abc")-PRE("aabc",“aabb”,"abc") =DP("aabc","aabb")+DP("abc")−PRE("aabc",“aabb”,"abc")
So let's consider S S S From its subset A A A Turn around , There is D P ( S ) = D P ( A ) + D P ( S − A ) + P R E ( S ) DP(S)=DP(A)+DP(S-A)+PRE(S) DP(S)=DP(A)+DP(S−A)+PRE(S).
Then you can do subsets d p dp dp 了 , Complexity o ( 3 n ) o(3^{n}) o(3n).
Code :
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int num1[1000005];
int getNum(int x)
{
if(num1[x])return num1[x];
int ans=0;
while(x!=0)
{
x-=x&-x;
ans++;
}
return num1[x]=ans;
}
int len[20];
int num[20][26];
char ss[1000005];
int n;
int pre(int s)
{
int nu[26];
for(int i=0;i<26;i++)nu[i]=1e9;
for(int i=0;i<n;i++)if((s>>i)&1)
{
for(int j=0;j<26;j++)nu[j]=min(nu[j],num[i][j]);
}
int sum=0;
for(int i=0;i<26;i++)sum+=nu[i];
return sum;
}
int dp[1000005];
int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%s",ss);
len[i]=strlen(ss);
for(int j=0;j<len[i];j++)num[i][ss[j]-'a']++;
}
for(int s=1;s<1<<n;s++)
{
dp[s]=1e9;
if(getNum(s)==1)
{
for(int i=0;i<n;i++)if((s>>i)&1)dp[s]=len[i];
continue;
}
int preS=pre(s);
for (int i=(s-1)&s;i;i=(i-1)&s)
{
dp[s]=min(dp[s],dp[i]+dp[s^i]-preS);
}
}
printf("%d\n",dp[(1<<n)-1]+1);
return 0;
}
版权声明
本文为[Zimba_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230621424840.html
边栏推荐
- Expansion of number theory Euclid
- ABAP publishes OData service samples from CDs view
- Three challenges that a successful Devops leader should be aware of
- 《數字電子技術基礎》3.1 門電路概述、3.2 半導體二極管門電路
- 501. Mode in binary search tree
- [Niuke practice match 68] fans of Niuniu (matrix fast power cycle matrix optimization)
- JS and how to judge custom attributes in H5
- Kernel PWN learning (4) -- double fetch & 0ctf2018 baby
- Creation of raid0 and RAID5 and Simulation of how RAID5 works
- Exclusive thoughts and cases of JS
猜你喜欢
Two methods of building Yum source warehouse locally
LeetCode 1611. The minimum number of operations to make an integer 0
501. Mode in binary search tree
Cloud identity is too loose, opening the door for attackers
STM32 and FreeRTOS stack parsing
High paid programmer & interview question series 91 limit 20000 loading is very slow. How to solve it? How to locate slow SQL?
Leetcode0587. Install fence
Easy to understand subset DP
Leetcode question bank 78 Subset (recursive C implementation)
Redis 异常 read error on connection 解决方案
随机推荐
Redis expired key cleaning and deletion policy summary
Nvidia最新三维重建技术Instant-ngp初探
Mobius inversion
STM32 and FreeRTOS stack parsing
ASUS laptop can't read USB and surf the Internet after reinstalling the system
Cloud identity is too loose, opening the door for attackers
Acquisition of DOM learning elements JS
Kernel PWN learning (3) -- ret2user & kernel ROP & qwb2018 core
[hdu6833] a very easy math problem
SAP RFC_ CVI_ EI_ INBOUND_ Main BP master data creation example (Demo customer only)
Integral function and Dirichlet convolution
Set the maximum width of the body, but why does the background color of the body cover the whole page?
Dropout技术之随机神经元与随机深度
AI上推荐 之 MMOE(多任务yyds)
如何实现根据照片获取地理位置及如何防御照片泄漏地理位置
Introduction to graph theory -- drawing
P1446 [hnoi2008] cards (Burnside theorem + DP count)
Exclusive thoughts and cases of JS
Go language learning notes - exception handling | go language from scratch
Leetcode0587. Install fence