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
题意:一个长度为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]);
}
}

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

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

  1. 【Trie图+DP】BZOJ1030[JSOI2007]-文本生成器

    [题目大意] 给出单词总数和固定的文章长度M,求出至少包含其中一个单词的可能文章数量. [思路] 对于至少包含一个的类型,我们可以考虑补集.也就是等于[总的文章可能性总数-不包含任意一个单词的文章总数 ...

  2. POJ2004 Mix and build Trie树? dp?

    学习Trie树中,所以上网搜一下Trie树的题,找到这个,人家写着是简单dp,那我就想着能学习到什么Trie树上的dp,但最后发现根本好像跟Trie树没有什么联系嘛... 题意就是给你很多个字符串(长 ...

  3. hdu 4570 Multi-bit Trie 区间DP入门

    Multi-bit Trie 题意:将长度为n(n <= 64)的序列分成若干段,每段的数字个数不超过20,且每段的内存定义为段首的值乘以2^(段的长度):问这段序列总的内存最小为多少? 思路: ...

  4. HDU4570:Multi-bit Trie(区间DP)

    Problem Description IP lookup is one of the key functions of routers for packets forwarding and clas ...

  5. hdu2457 Trie图+dp

    hdu2457 给定n个模式串, 和一个文本串 问如果修改最少的字符串使得文本串不包含模式串, 输出最少的次数,如果不能修改成功,则输出-1 dp[i][j] 表示长度为i的字符串, 到达状态j(Tr ...

  6. 【Luogu】P2536病毒检测(Trie上DP)

    题目链接 这道题我写了个01DP,f[i][j]表示跑到Trie上第i个节点,匹配到字符串第j位行不行 然后重点在*号无限匹配怎么处理 经过一番脑洞我们可以发现*号无限匹配可以拆成两种情况: 1:匹配 ...

  7. LA-3942(trie树+dp)

    题意: 给出一个由多个不同单词组成的字典,和一个长字符串,把这个字符串分解成若干个单词的连接,问有多少种方法; 思路: dp[i]表示s[i,L]的方案数,d[i]=∑d[j];s[i,j-1]是一个 ...

  8. NBUT 1222 English Game(trie树+DP)

    [1222] English Game 时间限制: 1000 ms 内存限制: 131072 K 问题描写叙述 This English game is a simple English words ...

  9. BZOJ1212: [HNOI2004]L语言(Trie图+DP)

    Description 标点符号的出现晚于文字的出现,所以以前的语言都是没有标点的.现在你要处理的就是一段没有标点的文章. 一段文章T是由若干小写字母构成.一个单词W也是由若干小写字母构成.一个字典D ...

随机推荐

  1. Windows phone 8 学习笔记(9) 集成

    原文:Windows phone 8 学习笔记(9) 集成 本节整理了之前并没有提到的Windows phone 8 系统相关集成支持,包括选择器.锁定屏幕的.联系人的访问等.选择器列举了若干内置应用 ...

  2. 认为C/C++很难理解、找工作面试笔试,快看看这本书!

    假设你是C/C++谁刚开始学习,看这本书.因为也许你读其他的书还不如不看.一定要选择一本好书. 假设你正在准备工作,请认真看这本书,由于这本书会教会你工作中必备的知识,相信你即将面临的语法类题目不会超 ...

  3. KMP算法---字符串匹配

    算法细节详见点击打开链接和点击打开链接 #include <stdio.h> #include <stdlib.h> #define N 7 #define M 15 void ...

  4. POJ1502(Dijkstra)

    MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5538   Accepted: 3451 题目链 ...

  5. javascript和css浏览器兼容性总结

    一些浏览器的兼容性做一个总结的问题以下: 为什么会出现这样的现象是?主要表现为Firefox这样的良好支持的浏览器W3C标准,这是现在CSS支持最好的浏览器,和ie它比较早出现,在w3c支持一直没有做 ...

  6. Linux 下 Error: Could not find or load main class Hello

    在linux下写了一个很easy的Hello world程序,编译执行居然报错:Error: Could not find or load main class Hello 最后发现是CLASSPAT ...

  7. 重新想象 Windows 8 Store Apps (6) - 控件之媒体控件: Image, MediaElement

    原文:重新想象 Windows 8 Store Apps (6) - 控件之媒体控件: Image, MediaElement [源码下载] 重新想象 Windows 8 Store Apps (6) ...

  8. 领域驱动设计(DDD)部分核心概念的个人理解(转)

    领域驱动设计(DDD)是一种基于模型驱动的软件设计方式.它以领域为核心,分析领域中的问题,通过建立一个领域模型来有效的解决领域中的核心的复杂问题.Eric Ivans为领域驱动设计提出了大量的最佳实践 ...

  9. mysql float double 类型

    1.float类型 float列类型默认长度查不到结果.必须指定精度. 比方 num float, insert into table (num) values (0.12); select * fr ...

  10. 【SICP读书笔记(四)】练习2.27 --- 表序列reverse的扩展:树结构的deep-reverse

    题目要求是,修改练习2.18所做的reverse过程,得到一个deep-reverse过程.它以一个表为参数,返回另一个表作为值,结果表中的元素反转过来,其中的子树也反转. 例如: (define x ...