hdu4570Multi-bit Trie (间隙DP)

Problem Description

  IP lookup is one of the key functions of routers for packets forwarding and classifying. Generally, IP lookup can be simplified as a Longest Prefix Matching (LPM) problem. That‘s to find the longest prefix in the Forwarding Information
Base (FIB) that matches the input packet‘s destination address, and then output the corresponding Next Hop information.

  Trie-based solution is the most wildly used one to solve LPM. As shown in Fig.1(b), an uni-bit trie is just a binary tree. Processing LPM on it needs only traversing it from the root to some leaf, according to the input packet‘s destination address. The longest
prefix along this traversing path is the matched one. In order to reduce the memory accesses for one lookup, we can compress some consecutively levels of the Uni-bit Trie into one level, transforming the Uni-bit Trie into a Multi-bit Trie.

  For example, suppose the strides array is {3, 2, 1, 1}, then we can transform the Uni-bit Trie shown in Fig.1(b) into a Multi-bit Trie as shown in Fig.1(c). During the transforming process, some prefixes must be expanded. Such as 11(P2), since the first stride
is 3, it should be expanded to 110(P2) and 111(P2). But 110(P5) is already exist in the FIB, so we only store the longer one 110(P5).

  Multi-bit Trie can obviously reduce the tree level, but the problem is how to build a Multi-bit Trie with the minimal memory consumption (the number of memory units). As shown in Fig.1, the Uni-bit Trie has 23 nodes and consumes 46 memory units in total,
while the Multi-bit Trie has 12 nodes and consumes 38 memory units in total.

Input

  The first line is an integer T, which is the number of testing cases.

  The first line of each case contains one integer L, which means the number of levels in the Uni-bit Trie.

  Following L lines indicate the nodes in each level of the Uni-bit Trie.

  Since only 64 bits of an IPv6 address is used for forwarding, a Uni-bit Trie has maximal 64 levels. Moreover, we suppose that the stride for each level of a Multi-bit Trie must be less than or equal to 20.

Output

  Output the minimal possible memory units consumed by the corresponding Multi-bit Trie.

Sample Input

1
7
1
2
4
4
5
4
3

Sample Output

38

Source

2013
ACM-ICPC长沙赛区全国邀请赛——题目重现

题意:一个长度为n的数列。将其分成若干段(每一段的长度要<=20),要求∑ai*(2^bi)最小,当中ai是每一段数列的第一项,bi是每一段的长度,

bi<=20。

#include<stdio.h>
__int64 min(__int64 a,__int64 b)
{
    return a>b?

b:a;
}
int main()
{
    int n,t;
    __int64 dp[100][100],ans[100];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%I64d",&ans[i]);

        for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        dp[i][j]=999999999;//注意初始化
        for(int len=0;len<n;len++)
        for(int i=1;i<=n-len;i++)
        {
            int j=i+len;
            if(len<20)
                dp[i][j]=ans[i]*(1<<(len+1));
            for(int k=i;k<j;k++)
            dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]);
        }
        printf("%I64d\n",dp[1][n]);
    }
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

时间: 2024-10-12 21:43:30

hdu4570Multi-bit Trie (间隙DP)的相关文章

Remember the Word,LA3942(Trie树+DP)

Trie树基础题,记录下代码. #include <cstdio> #include <cstring> #define MaxNode 4005*100 #define NodeSize 26 #define MOD 20071027 char givenword[300005]; int ans[300005]; int next[MaxNode][NodeSize]; class Trie{ public: int val[MaxNode]; int sz; Trie(){

hdu 4570 Multi-bit Trie 区间DP入门

Multi-bit Trie 题意:将长度为n(n <= 64)的序列分成若干段,每段的数字个数不超过20,且每段的内存定义为段首的值乘以2^(段的长度):问这段序列总的内存最小为多少? 思路:区间的最值,区间DP; 枚举长度,在初始化时,将长度和20比较,小于20看成是一段,大于20时,因为不能压缩,直接全部分割就是了:之后枚举区间内部的所有值,这是并不需要考虑将这个区间一分为二后各自的长度是否大于20,因为在子结构中已经计算好了:直接去最优解即可: #include<iostream>

hdu2457 Trie图+dp

hdu2457 给定n个模式串, 和一个文本串 问如果修改最少的字符串使得文本串不包含模式串, 输出最少的次数,如果不能修改成功,则输出-1 dp[i][j] 表示长度为i的字符串, 到达状态j(Trie图中的结点)所需要修改的最少次数 那么dp[0->n][0->size] = INF ,  dp[0][root] = 0,  n代表字符串长度, size代表状态数 那么答案就是  min{dp[n][size]} 我们根据模式串所建的Trie图, 进行模拟构造不包含模式串的字符串 从第一个

hdu4570Multi-bit Trie (区间DP)

Problem Description IP lookup is one of the key functions of routers for packets forwarding and classifying. Generally, IP lookup can be simplified as a Longest Prefix Matching (LPM) problem. That's to find the longest prefix in the Forwarding Inform

POJ 1625 Censored ( Trie图 &amp;&amp; DP &amp;&amp; 高精度 )

题意 : 给出 n 个单词组成的字符集 以及 p 个非法串,问你用字符集里面的单词构造长度为 m 的单词的方案数有多少种? 分析 :先构造出 Trie 图方便进行状态转移,这与在 POJ 2278 中的步骤是一样的,只不过最后的DP状态转移方式 2778 是利用了矩阵进行转移的,那是因为需要构造的串的长度非常长!只能利用矩阵转移.但是这道题需要构造的串的长度最多也就只有 50 ,可以利用普通的DP方法进行转移.我们定义 DP[i][j] 为以长度为 i 以字符 j 为结尾的串的种类数是多少,那么

ZOJ1463:Brackets Sequence(间隙DP)

Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular sequence. 2. If S is a regular sequence, then (S) and [S] are both regular sequences. 3. If A and B are regular sequences, then AB is a regular sequence. F

HNU 13108-Just Another Knapsack Problem (ac自动机上的dp)

题意: 给你一个母串,多个模式串及其价值,求用模式串拼接成母串(不重叠不遗漏),能获得的最大价值. 分析: ac自动机中,在字典树上查找时,用dp,dp[i]拼成母串以i为结尾的子串,获得的最大价值,dp[i]=max(dp[i],dp[i-len]+val[tmp]).,len是模式串的长度,val[tmp]为其价值. #include <cstdio> #include <cstring> #include <cmath> #include <queue>

uvaliva3942(trie树)

题目连接:https://vjudge.net/problem/UVALive-3942 trie树 dp[i]=sum(dp[i+len(x)]%mod; dp[i]表示从字符i开始的字符串的分解方案方案数,x是s[i--L]的前缀 1 #include<cstdio> 2 #include<cstring> 3 const int maxnode=500010; 4 const int sigma=26; 5 const int N=300010; 6 const int mo

[UVA]UVA1401 remember the name(Trie)

题目大意:给出一个长度为n的字符串(n<=3e5)和一个单词表(单词长度不超过100),求该字符串被这些单词表示的方案总数. 不难想到递推算法:令f[s]为表示字符串s的方案总数,若某个单词为s的前缀,f[s]=sum(f[x])(x为s去掉该单词前缀的后缀字符串) 寻找字符串的前缀是trie的经典操作,把所有单词插入trie中,然后在trie上dp即可. PE了三次... #include <bits/stdc++.h> using namespace std; typedef lon