Title Description
The stack, which is automatically allocated and released by the compiler, stores the function's parameter values, the values of local variables, and so on.
It operates like a stack in a data structure. The sum of the function call chain stacks is the sum of the size of each function stack in the call chain.
For example, if the entry function A allocates 100 bytes of stack space, A calls function B, B allocates 50 bytes of stack space, B calls function C, and C allocates 120 bytes of stack space, then the stack sum of A->B->C function call chain is
100+50+120=270.
Enter a description
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
input
Enter content description:
Line 1: Total number of call relationships n Number of first group of tuned functions 2 Number of tuned functions in group n Number of tuned functions in group n
Line 2: Call function 1 function 1 stack size Modulated function 1...Modulated function m
...
Line n+1: Call function n function n stack size Tuned function 1...Tuned function k
Up to 100 lines
5 2 3 1 0 // The first group calls two functions, the second group calls three functions, the third group calls one function, the fifth group calls no functions, and the fourth group calls no functions
1 20 2 3 // Function 1, stack size 20, calling functions 2, 3
2 303 4 5 // Function 2, stack size 30, calling functions 3, 4, 5
3 50 4
4 60
5 80
|
Output description:
1
2
3
4
|
1. Maximum sum of stacks in all call chains, note that entry functions may not be unique among inputs, such as 1 calling function 2, 3,5 calling 6,6 calling 7, or
There are three call chains, 1->2, 1->3, 5->6->7, with two entry functions 1 and 5.
2. If only one recursive call exists in all call chains (i.e. calling itself directly or indirectly, e.g. 1->2->3->1 is a recursive call), the output R
3. Output NA if there is a function in the call chain that does not give the call stack size
|
Example 1
input
1
2
3
4
5
6
|
5 2 3 1 0 0
1 20 2 3
2 30 3 4 5
3 50 4
4 60
5 80
|
output
1
|
160
|
Explain
1
2
3
4
5
6
|
-Function call chain:
- 1->2->3->4 Call chain stack sum 20+30+50+60=160
- 1->2->4 call chain stack sum 20+30+60=110
- 1->2->5 call chain stack sum 20+30+80=130
- 1->3->4 call chain stack sum 20+50+60=130
-The sum of stacks in all call chains is 1->2->3->4 and the maximum is 160, so the output is 160
|
Code implementation:
import java.util.*; /** * Author: chaoyou * CSDN: https://blog.csdn.net/qq_41910568 * Date: 23:15 2021/12/2 * Content: */ public class Call Stack { /** *Title Description * * The stack, which is automatically allocated and released by the compiler, stores the function's parameter values, the values of local variables, and so on. * * It works like a stack in a data structure. The sum of function call chain stacks is the sum of the size of each function stack in the call chain. * * For example, if the entry function A allocates 100 bytes of stack space, A calls function B, B allocates 50 bytes of stack space, B calls function C, and C allocates 120 bytes of stack space, then the stack sum of A->B->C function call chain is * * 100+50+120=270. * * Enter a description * * input * * Enter content description: * Line 1: Total number of call relationships n Number of first group of tuned functions 2 Number of tuned functions in group n Number of tuned functions in group n * Line 2: Call function 1 Function 1 Stack size adjusted function 1...Adjusted function m * ... * Line n+1: Call function n function n stack size adjusted function 1... adjusted function k * Up to 100 lines * 5 2 3 1 0 0 //5 Group calls the first group to call two functions, the second group to call three functions, the third group to call one function, the fifth group to call no functions, and the fourth group to call no functions * 1 20 2 3 //Function 1, stack size 20, calling functions 2, 3 * 2 30 3 4 5 //Function 2, stack size 30, calling functions 3, 4, 5 * 3 50 4 * 4 60 * 5 80 * Output description: * * 1.Maximum sum of stacks in all call chains, note that entry functions may not be unique among inputs, such as 1 calling function 2, 3,5 calling 6,6 calling 7, or * 1->2,1->3,5->6->7 Three call chains, two entry functions 1 and 5. * 2.If only one recursive call exists in all call chains (i.e. calling itself directly or indirectly, e.g. 1->2->3->1 is a recursive call), the output R * 3.Output NA if there is a function in the call chain that does not give the call stack size * * Example 1 * * input * 5 2 3 1 0 0 * 1 20 2 3 * 2 30 3 4 5 * 3 50 4 * 4 60 * 5 80 * * output * 160 * * Explain * * - Function call chain: * - 1->2->3->4 The total call chain stack is 20+30+50+60=160 * - 1->2->4 The total call chain stack is 20+30+60=110 * - 1->2->5 The total call chain stack is 20+30+80=130 * - 1->3->4 The total call chain stack is 20+50+60=130 * - The maximum stack sum in all call chains is 1->2->3->4 and the maximum is 160, so the output is 160 * * Solving ideas: * 1,Build all function stacks into a tall tree first * 1.1,Each node holds three variables: byte size, function number, and list of child nodes. * 2,Depth traversal through each path of a tall tree in a recursive manner * 2.1,Accumulate the byte size of this node for each path * 2.2,Each path goes to the end of the leaf node (i.e., the recursive exit) * 2.3,Collect byte accumulative values of byte nodes with a list * 3,Back list takes the first value and returns it as a result * * @param strArr * @return */ public static String tree(List<String> strArr){ if (null == strArr || strArr.size() < 1){ return "NA"; } // Get Function Group Information String one = strArr.get(0); String[] ones = one.split(" "); int methods = Integer.parseInt(ones[0]); /** * Construct a tamarisk tree */ // Step 1: First construct the information of all nodes: function number, subsection size Map<String, Map<String, Object>> nodeMap = new HashMap<>(); for (int i=1; i<=methods; i++){ Map<String, Object> node = new HashMap<>(); String temp = strArr.get(i); String[] tempList = temp.split(" "); if (tempList.length < 2){ return "NA"; } node.put("methodNo", tempList[0]); node.put("byte", tempList[1]); nodeMap.put(i+"", node); } // Start building the tree: perfect the child node logic of each node String root = ""; for (int i=1; i<=methods; i++){ String temp = strArr.get(i); String[] tempList = temp.split(" "); if (i == 1){ // Initialize Root Node Function Number root = tempList[0]; } // Remove the node based on the specified function number Map<String, Object> node = nodeMap.get(tempList[0]); // Initialize child node list List<Map<String, Object>> sunList = new ArrayList<>(); for (int j=2; j<tempList.length; j++){ Map<String, Object> sunNode = nodeMap.get(tempList[j]); sunList.add(sunNode); } // Parent node on child node list Association node.put("sunList", sunList); nodeMap.put(i+"", node); } List<Long> resultList = new ArrayList<>(); recursion(nodeMap.get(root), 0, resultList); resultList.sort(new Comparator<Long>() { @Override public int compare(Long o1, Long o2) { return o2.compareTo(o1); } }); System.out.println(resultList); return resultList.get(0)+""; } /** * Recursive deep traversal of tamarisks * @param treeMap * @param bytes * @param result */ public static void recursion(Map<String, Object> treeMap, long bytes, List<Long> result){ if (null == treeMap || treeMap.isEmpty() || null == result){ return; } // Get a list of child nodes List<Map<String, Object>> sunList = (List<Map<String, Object>>) treeMap.get("sunList"); // Gets the byte size of the current node and adds it up bytes += Long.parseLong(treeMap.get("byte")+""); // Determine whether the current node is a leaf node if (sunList.size() < 1){ // Save the path's byte accumulation value if it is a leaf node result.add(bytes); // Recursive Exit return; } // Recursively enter child nodes for (Map<String, Object> sunNode: sunList){ recursion(sunNode, bytes, result); } } public static void main(String[] args) { List<String> data = new ArrayList<>(); data.add("5 2 3 1 0 0"); data.add("1 20 2 3"); data.add("2 30 3 4 5"); data.add("3 50 4"); data.add("4 60"); data.add("5 80"); String tree = tree(data); System.out.println(tree); } }