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. 经常使用的正則表達式归纳—JavaScript正則表達式

    来源:http://www.ido321.com/856.html 1.正则优先级 首先看一下正則表達式的优先级,下表从最高优先级到最低优先级列出各种正則表達式操作符的优先权顺序: 2.经常使用的正則 ...

  2. ACdream原创群赛(18)のAK's dream题解

    只做了4题水题ADGI A题需要注意的就是“[...]”的输出了,何时输出,何时不输出. #include <stdio.h> int main() { int n, cur, d; ; ...

  3. 黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block 开发人员经常编写需要安全功能的应用程序.这些应用程序 ...

  4. coco2d-x 基于视口的地图设计

    <pre name="code" class="plain"> 基于视口的地图设计 DionysosLai 2014-06-14 第三人称游戏,玩家 ...

  5. 网站可以免费做业务CMS讨论

    中国现在用PHPCMS   DEDECMS织梦    科学新闻CMS  帝国.Discuz.Ecshop等待,但他们个人利益免费,业务.政府.授权费. 什么CMS它可以自由地做商务网站? 考虑到下面几 ...

  6. Ubuntu下用NdisWrapper安装网卡驱动

    下面是一个简单全面的使用NdisWrapper的指南.这是从Beginning Ubuntu Linux, Second Edition中提炼出来的. 这份指南是第8章的一部分.该章给出了在Ubunt ...

  7. The example program of C on point

    计划一: #include<stdio.h> #define N_VALUES 5 int main( void ) { float values[N_VALUES]; float *vp ...

  8. uip UDPclient模式通信移植,p本地ort可以是无规

    现在移植UDPclient模式,使用广播地址检测. //udp_client.c /********************************************************** ...

  9. Cluster Table

    对簇表来说,总是要先创建簇段(cluster segment).然后将表关联到cluster segment里.由此可知,簇表也是虚拟表,没有对应的segment,簇表对应的是cluster segm ...

  10. rsync 只是测试,请看下一篇

    实现从客户服务器去同步资源服务器 1.解压 # tar -xzpvf rsync-2.5.6.tar.gz   编译安装 # cd rsync-2.5.6/  # ./configure --pref ...