输入参数为字符串型的 n维数组,列表的每一项值为数组 或 int型数字。数组内的数组,每一项值,也可以是数组 或 int型数字。
int型数字,表示数组嵌套的深度。
[[1], [2,3,4], [5,[2,3]], [7], [0,[1,2,3,4],3,5], [1,3], [3,2,4]]
3
n维数组的深度为3
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
const int MAX_N = 1 << 16; // 65536
int main(const int argc, const char* const argv[]) {
char input[MAX_N] = "";
gets(input);
char stk[20];
int top = -1, ans = 0;
const char* p = input;
while (*p) {
switch (*p) {
case '[':
*(stk + ++top) = '[';
ans = fmax(ans, top + 1);
break;
case ']':
--top;
break;
default:
break;
}
++p;
}
assert(top == -1);
return fprintf(stdout, "%d", ans), 0;
}