当前位置:网站首页>[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
边栏推荐
- SAP 03-amdp CDs table function using 'with' clause
- LeetCode 1611. The minimum number of operations to make an integer 0
- PHP notes (I): development environment configuration
- ABAP publishes OData service samples from CDs view
- JS and how to judge custom attributes in H5
- [hdu6868] absolute math (pusher + Mobius inversion)
- ABAP CDs view with association example
- Cloud identity is too loose, opening the door for attackers
- MySQL of database -- basic common query commands
- C语言:表达式求值(整型提升、算术转换 ...)
猜你喜欢

MySQL of database -- Fundamentals

Summary of wrong questions 1

Redis exception read error on connection solution

Go language learning notes - language interface | go language from scratch

成功的DevOps Leader 应该清楚的3个挑战

Example of data object mask used by SAP translate

SAP salv14 background output salv data can directly save files and send emails (with sorting, hyperlink and filtering format)

kernel-pwn学习(4)--Double Fetch&&0CTF2018-baby

Leetcode题库78. 子集(递归 c实现)

SAP debug debug for in, reduce and other complex statements
随机推荐
个人主页软件Fenrus
Easy to understand subset DP
Simple understanding of arguments in JS
Two declaration methods of functions of JS
Leetcode0587. Install fence
阿里云架构师解读四大主流游戏架构
Compile and debug mysql8 with clion under MacOS x
Buuctf [actf2020 freshman competition] include1
Comparison of overloading, rewriting and hiding
MySQL - Chapter 1 (data type 2)
[lnoi2014] LCA - tree chain subdivision - multipoint LCA depth and problems
Leetcode question bank 78 Subset (recursive C implementation)
Base de la technologie électronique numérique 3.1 aperçu du circuit de porte, 3.2 circuit de porte à diode semi - conductrice
[reading notes] Chapter 5 conditional statements, circular statements and block statements of Verilog digital system design tutorial (with answers to thinking questions)
Summary of wrong questions 1
Practice of Flink streaming batch integration in Xiaomi
Two ways for flutter providers to share data
[codeforces - 208e] blood cousins
Redis exception read error on connection solution
Explanation of order and primitive root of number theory