HDU-4570 Multi-bit Trie (DP)
来源:互联网 发布:魅族note5 网络制式 编辑:程序博客网 时间:2024/05/19 22:27
Multi-bit Trie
http://acm.hdu.edu.cn/showproblem.php?pid=4570
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.
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.
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
171244543
Sample Output
38
题意太难懂了
准备直接看题解,结果一看大家都说是简单DP,就先回去好好看题
大致明白后,既然知道是dp,就往dp想了...想到一个很麻烦dp转移方程,dp[i]由dp[j]更新,其中再取后k个合并为一层,第j+1~(i-k)不合并,这其实是多此一举,dp[j]已经是前j层最小的内存占用,包括后几个不合并的情况
转移方程差不多是想到了,但是后k个合并为一层后占用的内存不知道该怎么算,又去看看题解,再结合题意,幡然醒悟:第j+1层有num[j+1]个结点,从j+1层开始合并len层为一层,则每个结点最多将会指向1<<len个结点,则共需要num[j+1]*(1<<len)大小的内存【感觉还是有点不明白】
最后状态转移方程为:dp[i]=min{dp[j]+num[j+1]*(1<<(i-j))},(max(0,i-20)<=j<i)
#include <cstdio>#include <algorithm>using namespace std;int l;long long num[65],dp[65];//dp[i]表示到第i层的最小内存占用int main() { int T; scanf("%d",&T); while(T-->0) { scanf("%d",&l); for(int i=1;i<=l;++i) { scanf("%I64d",num+i); dp[i]=(1LL<<42); } dp[0]=0; num[0]=1LL<<42; for(int i=1;i<=l;++i) { for(int j=max(i-20,0);j<i;++j) {//前j层的最小内存占用+ (j+1~i层合并为一层的内存占用) dp[i]=min(dp[i],dp[j]+num[j+1]*(1<<(i-j))); } } printf("%I64d\n",dp[l]); } return 0;}
0 0
- HDU-4570 Multi-bit Trie (DP)
- dp hdu-4570-Multi-bit Trie
- hdu 4570 Multi-bit Trie(dp水题)
- HDU 4570 - Multi-bit Trie(区间DP)
- HDU 4570 Multi-bit Trie (区间DP)
- HDU 4570 Multi-bit Trie(区间DP)
- hdu 4570 Multi-bit Trie 【区间dp】
- Hdu 4570 Multi-bit Trie(区间dp)
- HDU 4570 Multi-bit Trie(区间dp)
- hdu 4570 Multi-bit Trie (区间DP)
- hdu 4570 Multi-bit Trie
- hdu-4570-Multi-bit Trie-简单区间DP
- HDU 4570 Multi-bit Trie 解题报告
- HDU4570:Multi-bit Trie(区间DP)
- HDU4570----Multi-bit Trie----简单的DP
- HDU4570 Multi-bit Trie 区间DP
- hdu4570 Multi-bit Trie
- 【HDU4570】【Multi-bit Trie】
- java-工厂模式
- VC 中使用 WebBrowser 控件时自定义 User-Agent 的方法
- 求解二叉树的深度(递归和非递归)
- Android Activity学习笔记——Activity的启动和创建
- 富文本内图片 尺寸 宽度 自适应效果
- HDU-4570 Multi-bit Trie (DP)
- 【Developer Log】Thread-safe
- Android适配全攻略(学习笔记总结)
- 网卡的基础知识
- [精]免费SSL(https)证书申请之startssl
- js生成二维码实例(真实有效)
- Hbase javaapi curd笔记
- 焦距
- webserver.c Simple HTTP server sample for sanos