当前位置:网站首页>[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
边栏推荐
- Operation not allowed for a result set of type resultset TYPE_ FORWARD_ ONLY. Explain in detail
- Leetcode0587. Install fence
- Leetcode题库78. 子集(递归 c实现)
- php 二维数组指定元素相等后相加否则新增
- Sql1 [geek challenge 2019]
- Give the method of instantiating the object to the new object
- Secrets in buffctf file 1
- Kettle experiment
- [lnoi2014] LCA - tree chain subdivision - multipoint LCA depth and problems
- Simply understand = = and equals, why can string not use new
猜你喜欢

阿里云架构师解读四大主流游戏架构

#yyds干货盘点#ubuntu18.0.4安装mysql并解决ERROR 1698: Access denied for user ''root''@''localhost''

《數字電子技術基礎》3.1 門電路概述、3.2 半導體二極管門電路

Three challenges that a successful Devops leader should be aware of

Kernel PWN learning (3) -- ret2user & kernel ROP & qwb2018 core

Leetcode0587. Install fence

Flink 流批一体在小米的实践

重载、重写、隐藏的对比

从知识传播的维度对比分析元宇宙

Chinese Remainder Theorem and extended Chinese remainder theorem that can be understood by Aunt Baojie
随机推荐
SQL used query statements
Go language learning notes - structure | go language from scratch
DVWA range practice record
Three challenges that a successful Devops leader should be aware of
Flink 流批一体在小米的实践
Alibaba cloud architects interpret the four mainstream game architectures
golang力扣leetcode 396.旋转函数
防疫登记小程序
成功的DevOps Leader 应该清楚的3个挑战
Flutter's loading animation is more interesting
#yyds干货盘点#ubuntu18.0.4安装mysql并解决ERROR 1698: Access denied for user ''root''@''localhost''
Installation of data cleaning ETL tool kettle
Introduction to sap pi / PO login and basic functions
kettle庖丁解牛第14篇之JSON输入
Number theory blocking (integer division blocking)
Summary of wrong questions 1
Example of data object mask used by SAP translate
Two ways for flutter providers to share data
Dropout技术之随机神经元与随机深度
Base de la technologie électronique numérique 3.1 aperçu du circuit de porte, 3.2 circuit de porte à diode semi - conductrice