The following is a solution to some ACM algorithm questions I previously wrote on my live Space blog. It will be posted once for your reference and a record of my own research. In the future, I will publish new questions on the csdn blog and my live Space blog at the same time. 
 Recently, I decided to make some ACM questions to improve my algorithm level. N years (n> = 6) I haven't done this type of questions, and my hands are a little unfamiliar. There are a lot of ACM questions and online judge on the Internet. Among them, ultraviolet A is one of the most famous ones. Let's start from the perspective of ultraviolet. At that time, I should have worked on ACM at school. Unfortunately, after graduation, I remembered the following notes about ACM: 
 
 - Input and output are directly from stdin/stdout without reading and writing files.
- You can accept an input and output the answer immediately. You do not need to remember all the answers and then output them at one time.
- It is best to use gcc/g ++ to avoid compile error.
- Note whether the I/O termination condition is EOF or other special values.
 The question of the ultraviolet-Volume I-#100 is not difficult. You can obtain the answer by hard computing. However, dynamic programming is needed to quickly obtain the answer. To put it bluntly, we need to write down the intermediate results. Using arrays to remember is of course the simplest, but the memory requirements are quite large, so I will first use map to remember. To calculate the cycle number, recursion is required. I use a stack (vector) to remove recursion. First, the stack is pressed repeatedly until the answer is known, and then the stack is calculated in turn. After the program is written, is compile error submitted? I checked the online forum and did not find any useful suggestions. I tried # include old ones. the header file of H is still compile error. there is no way. I tried to change int main (void) to int main (INT argc, char * argv. But my GCC does not report an error? Compile error is good, and the next step is wrong answer. Strange, I have tried a lot of answers that should be correct. Try to consider the case of I> J or wrong answer. I looked at Forum and found that many people tried it n times in succession and it was wrong answer. Fortunately, some people pointed out that I j should not exchange order, but output as is. For example, if the output is 100 1, 100 1 is still to be output, and the figure cannot be exchanged directly to 1 100. Dizzy. The question clearly does not clarify these additional conditions. Do we have to guess for ourselves? After the modification, I fixed another bug and finally accepted. But that's time... It seems to be slower than their direct calculation and comparison of the maximum value. It may be because I use map to save the memory, which leads to slow searching. If array is used, it should improve a lot. BTW, I can see that many people in the top rankings actually have a CPU running time of 0 !?? Even if it is faster, it cannot be so outrageous. Will they calculate the results in advance and then output them directly? In the future, I will stick to some questions every week and post my own solutions. 
 Start with 101. This question is mainly used to simulate a robot's mobile square and does not require any skills. Use a list <int> As a stack to maintain the state of a pile of wood, and use such a vector to track the state of the building block heap at each position. Finally, a pos vector is used to track the position of each square. At the beginning, wrong answer found that he had read the wrong question request and modified it to accepted. The passing rate of this question is 25.7%, which may be caused by errors. The sub-Problem of the uva-# 103-stacking boxes is how to determine how a multi-dimensional box can be placed in another multi-dimensional box. In fact, you only need to sort the dimensions and compare them sequentially. There are two methods for this question: method 1) If box A can be placed in Box B, the node a corresponding to Box A has an edge to Node B corresponding to Box B. In this way, the problem is converted to finding the all pairs longest path. It can be solved through dynamic planning. 
Assume that dist [I, j] is the maximum distance from I to J, then Dist [I, j] = max {Dist [I, R] + 1 | G [R, j] <> max} The 0.088s method 2 used to calculate the test case of online judge is much faster than method 1. The question can be regarded as a strictly increasing number subsequence (longest scattered subsequence ). Suppose s [I] = subsequence ax1, ax2 ,... axm, XM = I, so s [I] = max {s [J] + 1 | A [J] <A [I]} is a typical bottom-up dynamic programming. it takes about 0.010 s to calculate the test case of online judge, which is about 9 times faster. 
  
Ultraviolet (a)-#104-arbitrage
 
I have finished it a few days ago, but I have no time to write it. To put it bluntly, it is nothing more than finding the shortest loop with the product of the maximum weight. Therefore, the most fundamental thing is to directly apply the Floyd-warshall algorithm. Because this question requires the shortest loop, it is not enough to use the two-dimensional f [I] [J] In the problem space, 3D is required: F [I] [J] [STEP]. Step indicates the length of the path. In addition to the three-tier loop of the Floyd-warshall algorithm, a step loop is required. The formula is as follows:
 
F [I] [J] [STEP] = max {f [I] [k] [step-1] * f [k] [J] [1] | f [K] [J] [1] <max}
 
This program and the last one both have presentation error. After trying it, I found that I had output a space after the last result and removed it to accept.
  
Ultraviolet A-#112-tree summing
 
It's actually a simple question. It's a bit strange why the accuracy rate is so low. Maybe you forget to consider negative numbers. Anyway can be solved directly by recursive descent. Recursive descent analysis tree structure is equivalent to traversing the entire tree, so you do not need to build this tree in the memory, but accumulate it at the same time during recursion, you can check whether such a path exists.
  
UV-#10405-Longest Common Sub-Sequence
 
I tried to solve this problem to consolidate my understanding of this algorithm. By the way, I also tested the speed difference between bottom-up dynamic programming and top-down dynamic programming. In general, if bottom-up dynamic programming does not calculate many child problems that are not useful, bottom-up dynamic programming is faster than top-down. This topic makes a comparison. There are spaces in the strings that are not mentioned at all ......
 
 
 
 
 
Author: Atfield
E-mail:Atfield_zhang@hotmail.com
Blog: Http://blog.csdn.net/atfield