当前位置:网站首页>[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
边栏推荐
- Golang force buckle leetcode 396 Rotation function
- Pre parsing of JS
- Integral function and Dirichlet convolution
- 重载、重写、隐藏的对比
- Two declaration methods of functions of JS
- JSON input of Chapter 14 of kettle paoding jieniu
- Sql1 [geek challenge 2019]
- JS case to find the maximum value, reverse the array, bubble sort
- C语言:表达式求值(整型提升、算术转换 ...)
- Simply understand = = and equals, why can string not use new
猜你喜欢

Practice of Flink streaming batch integration in Xiaomi

Kernel PWN learning (4) -- double fetch & 0ctf2018 baby

论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》——5结果

DVWA range practice

Enter "net start MySQL" and "system error 5. Access denied" appears. Detailed explanation of the problem

Leetcode0587. Install fence

SAP ECC connecting SAP pi system configuration

SAP excel has completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.

Chapter VIII project stakeholder management of information system project manager summary

三、6【Verilog HDL】基础知识之门级建模
随机推荐
P1390 sum of common divisor (Mobius inversion)
Go language learning notes - exception handling | go language from scratch
Random neurons and random depth of dropout Technology
Solving Lucas number and combination theorem
SAP ECC connecting SAP pi system configuration
(Extended) bsgs and higher order congruence equation
《數字電子技術基礎》3.1 門電路概述、3.2 半導體二極管門電路
ABAP implementation publishes restful services for external invocation example
Skill point digging
kettle庖丁解牛第14篇之JSON输入
Chapter VIII project stakeholder management of information system project manager summary
ABAP publishes OData service samples from CDs view
Leetcode题库78. 子集(递归 c实现)
Where is int a = 1 stored
Introduction to graph theory -- drawing
golang力扣leetcode 396.旋转函数
MySQL - Chapter 1 (data type 2)
JS DOM event
[ACM-ICPC 2018 Shenyang Network preliminaries] J. Ka Chang (block + DFS sequence)
DMP engine work summary (2021, lightsaber)