Multi-bit TrieTime
limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 539 Accepted Submission (s): 214
Problem Description IP Lookup is one of the keys 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 add Ress, 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-traversing it from the root to some leaf, according to the input packet ' s destination addr Ess. 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 was {3, 2, 1, 1}, then we can transform the Uni-bit Trie shown in Fig.1 (b) into a Mu Lti-bit Trie as shown in Fig.1 (c). During The transforming process, some prefixes must be expanded. Such as one (P2), since the first stride is 3, it should was expanded to (P2) and 111 (P2). But P5 are already exist in the FIB and so we only store the longer one (P5).
Multi-bit Trie can obviously reduce the tree level, but the problem are how to build a multi-bit Trie with the minimal memo Ry consumption (the number of memory units). As shown in Fig.1, the uni-bit Trie have nodes and consumes, memory units in total, while the multi-bit Trie have Des and consumes memory units in total.
Input The first line is a integer T, which is the number of testing cases.
The first line of all 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 a IPV6 address was used for forwarding, a uni-bit Trie have maximal levels. Moreover, we suppose the stride for each level of a multi-bit Trie must is 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
SOURCE2013 ACM-ICPC Changsha Division National Invitational--re-title
Recommend
/* Test Instructions: A series of length n, divides it into several segments (length of each paragraph to <=20), requires ∑ai* (2^BI) minimum, where AI is the first item of each column, BI is the length of each paragraph. For example: N=7,a={1 2 4 4 5 4 3}, divide it into 1 2 4| 4 5| 4| 3, the space used is 1*2^3+4*2^2+4*2^1+3*2^1=38, and if it is divided into 1 2| 4 4 5| 4 3, the space used is 1*2^2+4*2^3+4*2^2=52, which is larger than 38. Train: Range dp*/#include <iostream> #include <cstdio> #include <cstring> #include <algorithm># include<cmath> #include <queue> #include <stack> #include <vector> #include <set># Include<map> #define L (x) (x<<1) #define R (x) (x<<1|1), #define MID (x, y) ((x+y) >>1) #define Bug printf ("hihi\n") #define EPS 1e-8typedef __int64 ll;using namespace std; #define INF 0x3f3f3f3f#define n 65ll dp[n];ll a[n]; int N;int Main () {int i,j,t; scanf ("%d", &t); while (t--) {scanf ("%d", &n); for (i=1;i<=n;i++) {scanf ("%i64d", &a[i]); } memset (Dp,inf,sizeof (DP)); for (i=n+1;i<=n+20;i++) dp[i]=0; dp[n]=a[n]*2; for (i=n;i>=1;i--) For (J=i+1;j<=min (i+20,n+1); j + +)//At J This position separate {dp[i]=min (dp[i],a[i]* (1<<)) j-i]); } printf ("%i64d\n", dp[1]); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 4570 multi-bit Trie (interval dp)