I'm trying to solve https://open.kattis.com/problems/animal. My code can be found here : http://pastebin.com/xgXiDj91. I'm new to competitive programming and C++ in general. Having said this I have solved the Kattis Animal Classification challenge. We are given two classification trees, myeach represented using nested parentheses. For example, ((3,(1,(5,2))),4)
and (((5,1),(2,3)),4)
represent the trees
╱╲ ╱╲
╱╲ 4 ╱╲ 4
╱ ╱╲ ╱ ╲
3 1 ╱╲ ╱╲ ╱╲
5 2 5 1 2 3
The challenge is to report the fact that there are 7 subgroups in common between the two trees (namely {1}, {2}, {3}, {4}, {5}, {1,2,3,5}, and {1,2,3,4,5}).
My approach is to parse the input string [ e.g. ((3,(1,(5,2))),4)
] into a list [ ((3(1(52)))4)
]. After that iI note all elements, split it into the two subtrees and add both back to a queue, starting over again, until there is nothing to split anymore. Afterwards i compare the two sets i obtained this way. If i am not mistaken this should lead to a complexity of O(Nlog(N))
O(N log(N)), since for every subtree, i need to collect all elements that belong to it, which would amount to Nlog(N)
N log(N) in a complete binary tree. This works for the provided test sets, but exceeds the time limit on the submission.
Could you please help me figure out where iI can improve my code and what iI did inefficiently?