博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU4570:Multi-bit Trie(区间DP)
阅读量:5745 次
发布时间:2019-06-18

本文共 3624 字,大约阅读时间需要 12 分钟。

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
 

 

题意:这题题意确实有点难懂,起码对于我这个英语渣渣来说是这样,于是去别人的博客看了下题目意思,归纳起来如下:

给出一个长度为n的数列,将其分成若干段,要求最小,其中ai是每一段数列的第一项,bi是每一段的长度,l为将数列分成l段。

比如样例:n=7,A={1 2 4 4 5 4 3},将其分成1 2 4| 4 5| 4| 3,则其所用空间为1*2^3+4*2^2+4*2^1+3*2^1=38,而如果分成1 2| 4 4 5| 4 3,则其所用空间为1*2^2+4*2^3+4*2^2=52,比38大。

 

思路:区间DP,

dp[i][j]表示i--j层最小的内存;

初始条件:全压缩或全不压缩

因为压缩不能超过20层,所以在小于20层时初始条件:

dp[i][j]=num[i]*pow(j-i)*2;

大于20层是只能不压缩

dp[i][j]=(sum[j]-sum[i-1])*2; 

然后循环

dp[i][j]=min(dp[i][k]+dp[k+1][j],dp[i][j]); k:i...j;

 

 

#include 
#include
#include
using namespace std;int n;__int64 dp[70][70],a[70],sum[70];__int64 pow(__int64 n){ __int64 ans= 1; int i; for(i = 1; i<=n; i++) ans*=2; return ans;}int main(){ int t,i,j,k,s; scanf("%d",&t); while(t--) { scanf("%d",&n); memset(sum,0,sizeof(sum)); for(i = 1; i<=n; i++) { scanf("%I64d",&a[i]); sum[i] = sum[i-1]+a[i]; } memset(dp,0,sizeof(dp)); for(s = 0; s<=n; s++) { for(i = 1; i<=n && i+s<=n; i++) { j = i+s; if(s<=19)//小于20层,全压缩 dp[i][j] =a[i]*pow(j-i)*2; else//多于20,全不压缩 dp[i][j] = (sum[j]-sum[i-1])*2; for(k = i; k<=j; k++)//区间dp dp[i][j] = min(dp[i][j],dp[i][k]+dp[k+1][j]); } } printf("%I64d\n",dp[1][n]); } return 0;}

你可能感兴趣的文章
存储过程简单实例
查看>>
大话 程序猿 眼里的 接口
查看>>
struts2用了哪几种模式
查看>>
replace函数结合正则表达式实现转化成驼峰与转化成连接字符串的方法
查看>>
ubuntu 初学常用命令
查看>>
num+=num 与 num = num+num
查看>>
WCF客户端与服务端通信简单入门教程
查看>>
判断是否含有中文
查看>>
iOS开发UI篇—程序启动原理和UIApplication
查看>>
CAlayer(创建图层)
查看>>
android 学习随笔二十七(JNI:Java Native Interface,JAVA原生接口 )
查看>>
网站迁移至win2008r2系统II7.5以后,样式和图片都加载不了的问题
查看>>
问题解决:python3 socket服务端发送html文件,火狐浏览器打开,源码以文本形式显示...
查看>>
OpenStack cloudCompute glassary术语project,tenant,user
查看>>
ubunt 基于deb 配置本地apt 源 分成仅本机使用,局域网使用2种
查看>>
ios 界面间跳转方法总结
查看>>
通过python3学习编码
查看>>
hdu3549 ek模板
查看>>
mysql 索引( mysql index )
查看>>
Wcf 基础编程
查看>>