File tree 2 files changed +69
-6
lines changed 2 files changed +69
-6
lines changed Original file line number Diff line number Diff line change
1
+ # SHIV's code for duplicate sub tree
2
+
3
+
4
+ '''
5
+ 2
6
+ (a(b(d()())(e()()))(c()(b(d()())(e()()))))
7
+ (a(b()())(c()()))
8
+ '''
9
+ import re
10
+
11
+ tn = int (input ())
12
+
13
+ def next_ind (c , tree ):
14
+ if c == '-' : return - 1
15
+ if c not in tree : return - 1
16
+ return tree .index (c )
17
+
18
+ def is_dup (tree ):
19
+ index_bool = [False for i in tree ]
20
+ # skip the root
21
+ index_bool [0 ] = True
22
+ index_bool [1 ] = True
23
+
24
+ for ind , vis in enumerate (index_bool ):
25
+ if vis :continue
26
+ index_bool [ind ] = True
27
+ if tree [ind ] == '(' :
28
+ continue
29
+ elif tree [ind ] != ')' :
30
+ ind2 = next_ind (tree [ind ], tree [ind + 1 :])
31
+ if ind2 != - 1 :
32
+ ind2 = ind2 + ind
33
+ # visit the opening paranthesis
34
+ index_bool [ind2 ] = True
35
+ ind2 += 1
36
+ index_bool [ind2 ] = True
37
+ else :continue
38
+
39
+ #initialize a stack
40
+ stack = []
41
+ stack .append (tree [ind ])
42
+ i ,j = ind , ind2
43
+ while stack :
44
+ i += 1
45
+ j += 1
46
+ try :
47
+ if index_bool [i ] or index_bool [j ]:continue
48
+ except :
49
+ break
50
+ index_bool [i ], index_bool [j ] = True , True
51
+ if tree [i ] == tree [j ]:
52
+ if tree [i ] == '(' :continue
53
+ elif tree [i ] == ')' :
54
+ del stack [- 1 ]
55
+ else :
56
+ stack .append (tree [i ])
57
+ if not stack :return True
58
+ return False
59
+
60
+
61
+
62
+ for i in range (tn ):
63
+ tree = input ()
64
+ tree = re .sub ('\(\)' , '(-)' , tree )
65
+ ans = is_dup (tree )
66
+ print (ans )
67
+ print ("----------------------------" )
Original file line number Diff line number Diff line change 1
1
2
2
- 12
3
- i like sam sung samsung mobile ice cream icecream man go mango
4
- ilike
5
- 12
6
- i like sam sung samsung mobile ice cream icecream man go mango
7
- idontlike
2
+ (a(b(d()())(e()()))(c()(b(d()())(e()()))))
3
+ (a(b()())(c()()))
You can’t perform that action at this time.
0 commit comments