首页 > 代码库 > uva 10562 - Undraw the Trees
uva 10562 - Undraw the Trees
Problem D
Undraw the Trees
Input:StandardInput
Output: Standard Output
Time Limit: 2 Seconds
Professor Homer has been reported missing. We suspect thathis recent research works might have had something to with this. But we reallydon‘t know much about what he was working on! The detectives tried to hack intohis computer, but after hours of failed efforts they realized that theprofessor had been lot more intelligent than them. If only they could realizethat the professor must have been absent minded they could get the clue rightaway. We at the crime lab were not at all surprisedwhen the professor‘s works were found in a 3.5"floppy disk left inside the drive.
The disk contained only one text file in which the professorhad drawn many trees with ASCIIcharacters. Before we can proceed to the next level of investigation we wouldlike to match the trees drawn with the ones that we have in our database. Nowyou are the computer geek -- we leave this trivial task for you. Convertprofessor‘s trees to our trees.
Professor‘sTrees
The first line of the input file (which you can assume comesfrom standard input) contains the number of trees, T (1 <= T <= 20) drawn in the file. Then you would have T trees, each ending with a single hash(‘#‘) mark at the beginning of theline. All the trees drawn here are drawn vertically in top down fashion. Thelabels of each of node can be any printable character except for the symbols ‘-‘, ‘|‘, ‘ ‘(space) and ‘#‘. Every node that has children has a ‘|‘ symbol drawn just below itself. And in the next line therewould be a series of ‘-‘ marks atleast as long as to cover all the immediate children. The sample input sectionwill hopefully clarify your doubts if there is any. No tree drawn here requiresmore than 200 lines, and none ofthem has more than 200 characters inone line.
OurTrees
Our trees are drawn with parenthesis and the node labels.Every subtree starts with an opening parenthesis andends with a closing parenthesis; inside the parenthesis the node labels arelisted. The sub trees are always listed from left to right. In our databaseeach tree is written as a single string in one line, and they do not containany character except for the node labels and the parenthesis pair. The nodelabels can be repeated if the original tree had such repetitions.
Sample Professor’sTrees Corresponding Our Trees
2 A | -------- B C D | | ----- - E F G # e | ---- f g # | (A(B()C(E()F())D(G()))) (e(f()g()))
|
#include <iostream>#include <stack>#include <cstring>#include <cstdio>#include <string>#include <algorithm>#include <queue>#include <set>#include <map>#include <fstream>#include <stack>#include <list>#include <sstream>#include <cmath>using namespace std;#define ms(arr, val) memset(arr, val, sizeof(arr))#define mc(dest, src) memcpy(dest, src, sizeof(src))#define N 205#define INF 0x3fffffff#define vint vector<int>#define setint set<int>#define mint map<int, int>#define lint list<int>#define sch stack<char>#define qch queue<char>#define sint stack<int>#define qint queue<int>char s[N][N];int t, n, pos[N];void dfs(int i){ //putchar(‘(‘); int j = i + 1; bool tag = false; for (; s[j][pos[j]]; pos[j]++) { if (s[i][pos[j]] == ‘-‘ && s[j][pos[j]] != ‘#‘ && s[j][pos[j]] != ‘ ‘ && s[j][pos[j]] != ‘-‘ && s[j][pos[j]] != ‘|‘) { tag = true; putchar(s[j][pos[j]]); putchar(‘(‘); if (s[j + 1][pos[j]] == ‘|‘) { dfs(i + 3); } putchar(‘)‘); } else if (tag && s[i][pos[j]] != ‘-‘)//特别注意 { tag = false; return; } } //putchar(‘)‘);}int main(){ ms(s[0], ‘-‘); scanf("%d", &t); getchar(); while (t--) { n = 1; while (gets(s[n]), s[n][0] != ‘#‘) n++; ms(s[n], 0); ms(pos, 0); putchar(‘(‘); dfs(0); putchar(‘)‘); putchar(‘\n‘); } return 0;}