Hdu4570Multi-bit Trie (interval DP), interval dp

Source: Internet
Author: User

Hdu4570Multi-bit Trie (interval DP), interval dp
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 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]);}



The common algorithm used to teach ACM is cainiao.

Good post. Pin it down.

First, you must learn a computer language, and then learn data structures and algorithms.

Finally, you need to exercise more on your own.

I recommend a few books for my freshman year ACM,

I am a junior! I started ACM in my first year of college. I think you may be eligible to download a set of Hangzhou-electric ACM Courseware in Xinhua, Hangzhou.
PPT, you will be greatly improved! What does it mean to search online! Now there are a lot of online Q &! I think there are too many resources on the Internet. Than a good book, not for money! You will be quickly improved, paying attention to the details, and providing constant power to help you determine whether the problem is correct or not. Sometimes you think your own problem is correct, but not completely. Hangdian can help you understand it. In fact, many of the answers to the game's time are correct, but I cannot go up and the Details determine success or failure. A good start, from freshman year in Hangzhou Xinhua has the right to do so. You will become a master!

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.