Scroll array ~ \ (Too many rows )/~

Source: Internet
Author: User

The first time I used a rolling array today, I want to start with a question: poj 1159 palindrome

Question: To give you a string, you must add at least a few characters to the string to be converted into a return string.

Analysis: the simple method is to directly calculate the longest common subsequence length Len from its reverse sequence string. N-Len is the request. As for why, you can simulate it yourself. O (distinct _ distinct) O ~ This is not the focus of our speech today ~ Xiaowei

Obviously, this is an LCS question. If you do not know what LCS is, click LCS We all know that we want to open up an array of DP [5001] [5001] to store the LCS value. The problem is that when I happily handed in the code

Memory exceeded ~!! After reading the discussion group, I can change it to a short Int. It's really good, but memory = 49156... It's too big. When I searched the internet and found the scroll array, memory immediately programmed 188 K. Let's get started!

The function of rolling an array isOptimized Space, Mainly used in recursive or dynamic planning (for example, 01 backpack problem ). Because DP is a bottom-up expansion process, we often need continuous solutions.

Remove. Therefore, it is very effective to use the scrolling array optimization. The scrolling array can compress the storage with N values.

If a DP requires 1000x1000 space, it can be scaled to 2x1000 based on the non-efficiency of the DP, and then scroll to get the same effect as 1000x1000.

Scrolling arrays are often used in DP. During the DP process, when we switch from one status to another,It is very likely that some of the previously stored status information is useless.For example, in question 01, we should open

The two-dimensional array of DP [I] [J]. we store the first item in the first dimension, that is, the stage. The second-dimensional storage capacity,We get the DP [I]. We only need to use the DP [I-1] information. DP [I-K] and K> 1 become useless space., So we

You can split the array into one dimension. Iterative update of the content in the array. This is also the principle of rolling the array, and the goal is the same. However, the problem at this time is often unable to be reduced to one dimension, for example, a DP [I] [J] needs to be composed of DP [I-1] [K],

DP [I-2] [k] determines that I <n, 0 <k <= 10; n <= 100000000; obviously, it cannot be reduced to one dimension, normally, we should open an array of DP [100000005] [11]. The result is obvious. It is super memory. In fact, we only need to enable DP [3] [11].

This is enough for DP [I % 3] [J] By DP [(I-1) % 3] [k] and DP [(I-2) % 3] [k] determines that the space complexity varies greatly.

Application: http://poj.org/problem? Id = 1159 let's do it now ~ Xiaoxu ~

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.