Solutions to the acm oj big data recursive stack overflow problem

Source: Internet
Author: User

Solutions to the acm oj big data recursive stack overflow problem
Posted This_poet On
2012-08-15
Edit  
Problem Source

When I give a simulated question to the noip students, I will not go to the questions that my school students, such as BNU and HDU, do not often go to the question library to strengthen or adapt. Today, I found a question on BNU contest, which involves the need to shrink the ring to a point. Obviously, recursive Tarjan is the most convenient choice for reducing loops. However, sometimes the data range in the question is n <= 100000 or greater. If the figure is a chain,ProgramStack Overflow is certainly caused by recursion. This article describes how to solve this problem.


Solution

For memory restrictions, except for the sgu OJ, the limitations given by each question bank are generally very loose (except for card memory questions), with a minimum of 32 MB (32768kb ). However, the stack space of most evaluation machines is limited to only 2 MB, which may be 1 MB or even smaller. Therefore, we can consider allocating the remaining memory to the stack space to solve this stack overflow problem. For the PASCAL Language, the $ M syntax can solve this problem. We only need to add the following sentence before the program:


View
Source 
Print?

1 {$ M 100000000}


In this way, the general stack overflow problem can be solved. Simply put, it is a comment statement. Do not underestimate "$". This symbol makes the entire statement a command. Of course, if your program falls into infinite recursion, there is no way to use this statement automatically. This statement is used to solve the problem in a recursive (terminated) manner. The data is large and the occupied space is large. However, the stack memory allocation is not large enough. In essence, the stack memory size is allocated manually.

In PASCAL, the problem was solved successfully. However, for the majority of C ++ language players, the problem is still not solved. Is there any similar syntax in C ++ language?

The answer is yes!

For the C ++ language, we can add the following command before the program:


View
Source 
Print?

1 # Pragma comment (linker, "/Stack: 1024000000,1024000000 ")


What does this statement mean? I quoted an explanation on Baidu encyclopedia as follows:

# Pragma comment (comment-type, ["commentstring"])

Comment-type is a predefined identifier that specifies the annotation type. It should be one of compiler, exestr, Lib, and linker.

Commentstring is a string that provides additional information for comment-type.

In fact, this command is the same as the $ M statement in the PASCAL Language described above. The purpose is to manually allocate memory to the stack memory. The two statements described in this article can allocate the free memory in the memory to the stack so that the stack will not overflow during normal operations.

Related Article

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.