Hdu4570Multi-bit trie (interval DP)

Source: Internet
Author: User
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 shocould 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 obviusly 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
171244543

Sample output
38

Source2013 ACM-ICPC Changsha Division national invitational competition-Questions reproduction: a length of N Series, divided into several segments (the length of each segment is <= 20 ), ∑ ai * (2 ^ Bi) is required to be the minimum, where Ai is the first item in each series, Bi is the length of each segment, 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; // initialize 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 (interval DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.