利用中序和后序构建二叉树然后进行dfs搜索,两步可以同时进行,另外这道题目输入处理也有点麻烦,参考了一些大神的代码,自己重写因为一个值每次case之后未初始化悲剧的RE了好多次。
查了下 runtime error的可能大概有:
Runtime Error(ARRAY_BOUNDS_EXCEEDED) // array bounds exceed 数组越界
Runtime Error(DIVIDE_BY_ZERO) //divisor is nil 除零
Runtime Error(ACCESS_VIOLATION) //illegal memory access 非法内存读取
Runtime Error(STACK_OVERFLOW) //stack overflow 系统栈过载
#include<stdio.h> int in[10010]; int post[10010]; int minSum; int res; int findIndex(int key, int*array, int n) { int i; for (i = 0; i < n; i++) { if (array[i] == key) return i; } return -1; } void dfs(int n, int*in, int*post, int sum) { if (n <= 0) return; int root = post[n - 1]; if (n == 1) { int tmpSum = sum + root; if (tmpSum < minSum) { minSum = tmpSum; res = root; } else if (tmpSum == minSum) res = res < root ? res : root; return; } int index = findIndex(post[n - 1], in, n); dfs(index, in, post, sum + root); dfs(n - index - 1, in + index + 1, post + index, sum + root); } int main() { /* setbuf(stdout,NULL);*/ char ch; int i, n; while (scanf("%d%c", &in[0], &ch) != EOF) { minSum = 0x7fffffff; res = 0x7fffffff; if (ch == '\n') { scanf("%d", &post[0]); printf("%d\n", post[0]); continue; } n = 1; while (scanf("%d%c", &in[n++], &ch)) { if (ch == '\n') break; } for (i = 0; i < n; i++) scanf("%d", &post[i]); dfs(n, in, post, 0); printf("%d\n", res); } return 0; }
相关推荐
判断输入字符串是否为镜像或回文串。 来源于UVaOJ - 401. 水题。
开源项目-codingsince1985-UVa#uva-online-judge-solutions-in-golang.zip,两年来每天都在解决一个uva在线裁判问题,算起来…
uva705 Slash Maze 的代码,在UVaOJ上通过
PDF试题
uva532 Dungeon Master的源代码,并且AC了
Algorithm-UVA-Solutions-in-Python.zip,python 3中各种uva(acm)问题的解决方案。,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
这是UVA133 TheDoleQueue救济金发放问题,经典的算法问题。初学算法的人要对这种算法非常熟悉并且能熟练运用。
tpcw-nyu-uva-client 客户端
leetcode 2 算法-Java UVa Online Judge(ACM-ICPC Live ...使用:数组、哈希表、链表、二分搜索、动态规划、堆栈、堆、reedy、排序、树 DFS、BFS、图、二分搜索树、递归、记忆、队列、映射等。...Uva-ACM-ICPC