HDU 4570 Multi-bit Trie (区间DP)
来源:互联网 发布:java面试数据结构算法 编辑:程序博客网 时间:2024/05/07 23:30
Multi-bit Trie
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 803 Accepted Submission(s): 342
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的数列,将其分成若干段,要求最小,其中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大。
#include "cstring"#include "string.h"#include "iostream"#include "algorithm"#include "cstdio"using namespace std;long long a[100];long long dp[100][100];long long sum[100];int main(){ int cas; scanf("%d",&cas); while(cas--) { int n; scanf("%d",&n); memset(dp,0,sizeof(dp)); sum[0]=0; for(int i=1;i<=n;i++) { scanf("%lld",&a[i]); sum[i]=sum[i-1]+a[i]; } for(int len=1;len<=n;len++) { for(int i=1;i+len-1<=n;i++) { if(len<=20) dp[i][i+len-1]=a[i]*(1<<len); else dp[i][i+len-1]=(sum[i+len-1]-sum[i-1])*2; for(int j=i;j<=i+len-1;j++) dp[i][i+len-1]=min(dp[i][i+len-1],dp[i][j]+dp[j+1][i+len-1]); } } printf("%lld\n",dp[1][n]); }}
- 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)
- dp hdu-4570-Multi-bit Trie
- hdu 4570 Multi-bit Trie(dp水题)
- HDU-4570 Multi-bit Trie (DP)
- HDU4570:Multi-bit Trie(区间DP)
- HDU4570 Multi-bit Trie 区间DP
- hdu 4570 Multi-bit Trie
- HDU 4570 Multi-bit Trie 解题报告
- hdu4570Multi-bit Trie (区间DP)
- HDU4570----Multi-bit Trie----简单的DP
- hdu4570 Multi-bit Trie
- day19字符流: FileReader FileWriter (BufferedReader BufferedWriter ) 字节流: InputStream OutputStream读取键盘录入
- ACM学习历程23——最小周期串问题
- CodeForces 369 div2 E. ZS and The Birthday Paradox 数论
- Android网络请求库 - Say hello to retrofit
- 伊为君心空流泪
- HDU 4570 Multi-bit Trie (区间DP)
- Unity 5 中的全局光照技术详解(建议收藏)
- 在Angularjs中使用百度地图
- LR参数化类型为file显示大于100数据方法
- TabBarItem设置图片超出tabbar范围
- RimWorld模组教程之武器
- css3
- DIV+CSS解决IE6,IE7,IE8,FF兼容问题
- MYSQL启用日志,查看日志,利用mysqlbinlog工具恢复MySQL数据库