当前位置:网站首页>[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
边栏推荐
- LeetCode 1611. The minimum number of operations to make an integer 0
- 1D / 1D dynamic programming learning summary
- What is monitoring intelligent playback and how to use intelligent playback to query video recording
- JS scope, scope chain, global variables and local variables
- DVWA range practice record
- SAP CR transmission request sequence and dependency check
- 亚马逊云科技入门资源中心,从0到1轻松上云
- GCD of p2257 YY (Mobius inversion)
- JSON input of Chapter 14 of kettle paoding jieniu
- 元宇宙时代的职业规划与执行
猜你喜欢
三、6【Verilog HDL】基础知识之门级建模
kettle庖丁解牛第14篇之JSON输入
Canary publishing using ingress
个人主页软件Fenrus
What is monitoring intelligent playback and how to use intelligent playback to query video recording
[COCI] lattice (dichotomy + tree divide and conquer + string hash)
DVWA range practice
Exclusive thoughts and cases of JS
Go language learning notes - exception handling | go language from scratch
JS what is an event? Event three elements and operation elements
随机推荐
Mobius inversion
SAP salv14 background output salv data can directly save files and send emails (with sorting, hyperlink and filtering format)
Kettle experiment (III)
SAP 03-amdp CDs table function using 'with' clause
SQL used query statements
[Niuke practice match 68] fans of Niuniu (matrix fast power cycle matrix optimization)
php 二维数组指定元素相等后相加否则新增
#yyds干货盘点#ubuntu18.0.4安装mysql并解决ERROR 1698: Access denied for user ''root''@''localhost''
Educational Codeforces Round 81 (Rated for Div. 2)
[ACM-ICPC 2018 Shenyang Network preliminaries] J. Ka Chang (block + DFS sequence)
面试官:说几个PHP常用函数,幸好我面试之前看到了这篇文章
Three challenges that a successful Devops leader should be aware of
DVWA range practice
kernel-pwn学习(3)--ret2user&&kernel ROP&&QWB2018-core
Go language learning notes - exception handling | go language from scratch
Solving Lucas number and combination theorem
Operation not allowed for a result set of type resultset TYPE_ FORWARD_ ONLY. Explain in detail
Two ways for flutter providers to share data
STM32 and FreeRTOS stack parsing
Three ways to create objects in JS