File tree Expand file tree Collapse file tree 2 files changed +69
-0
lines changed
Expand file tree Collapse file tree 2 files changed +69
-0
lines changed Original file line number Diff line number Diff line change 1+ import sys
2+
3+ def main ():
4+ input = sys .stdin .readline
5+ N = int (input ())
6+
7+ for i in range (N ):
8+ word = str (input ().strip ())
9+
10+ word = sorted (list (word ))
11+
12+ result = []
13+ backtracking (word , [], [False ] * len (word ), result )
14+
15+ for r in result :
16+ print (r )
17+
18+ def backtracking (word , path , visited , result ):
19+ if len (path ) == len (word ):
20+ result .append ("" .join (path ))
21+ return
22+
23+ for i in range (len (word )):
24+ if visited [i ]:
25+ continue
26+ if i > 0 and word [i ] == word [i - 1 ] and not visited [i - 1 ]:
27+ continue
28+
29+ visited [i ] = True
30+ path .append (word [i ])
31+ backtracking (word , path , visited , result )
32+ path .pop ()
33+ visited [i ] = False
34+
35+ if __name__ == '__main__' :
36+ main ()
Original file line number Diff line number Diff line change 1+ import sys
2+ sys .setrecursionlimit (10 ** 7 )
3+
4+ def main ():
5+ input = sys .stdin .readline
6+ S = input ().strip ()
7+ T = input ().strip ()
8+ found = False
9+
10+ def bt (cur : str ):
11+ nonlocal found
12+ if found :
13+ return
14+
15+ if len (cur ) < len (S ):
16+ return
17+
18+ if len (cur ) == len (S ):
19+ if cur == S :
20+ found = True
21+ return
22+
23+ if cur .endswith ('A' ):
24+ bt (cur [:- 1 ])
25+
26+ if cur .startswith ('B' ):
27+ bt (cur [1 :][::- 1 ])
28+
29+ bt (T )
30+ print (1 if found else 0 )
31+
32+ if __name__ == '__main__' :
33+ main ()
You can’t perform that action at this time.
0 commit comments