https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1503
这道题错了好多次,一开始我直接是cin>>t,但前面可能还有空格,所以不对。就按照书上的用了fgets(buf[0],maxn,stdin)。
题目的本质就是二叉树的遍历问题,直接运用dfs遍历即可。
1 #include<iostream> 2 #include<cstring> 3 #include<cctype> 4 using namespace std; 5 6 const int maxn = 220; 7 char buf[maxn][maxn]; 8 int n; 9 10 void dfs(int r, int i) 11 { 12 cout << buf[r][i]; 13 cout << "("; 14 if (r + 1<n && buf[r + 1][i] == '|') 15 { 16 int k = i; 17 while (k - 1 >= 0 && buf[r + 2][k - 1] == '-') k--; 18 while (buf[r + 2][k] == '-' && buf[r+3][k]!='