icpc probs
This commit is contained in:
parent
4c531b965f
commit
69d48861b1
17 changed files with 448 additions and 2 deletions
46
kattis/23-9-24/d/solution.py
Normal file
46
kattis/23-9-24/d/solution.py
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
def solve(equation: str) -> None:
|
||||
stack = []
|
||||
paren_pairs = []
|
||||
|
||||
for i, token in enumerate(equation):
|
||||
if token == "(":
|
||||
stack.append([i, None])
|
||||
elif token == ")":
|
||||
l, r = stack.pop()
|
||||
r = i
|
||||
paren_pairs.append((l, r))
|
||||
|
||||
P = [[]]
|
||||
|
||||
for paren_pair in paren_pairs:
|
||||
P.extend([[paren_pair] + p for p in P])
|
||||
|
||||
def format(permutation):
|
||||
output = list(equation)
|
||||
|
||||
for l, r in permutation:
|
||||
output[l] = None
|
||||
output[r] = None
|
||||
|
||||
return "".join(filter(lambda token: token, output))
|
||||
|
||||
seen = set()
|
||||
ans = []
|
||||
for permutation in P[1:]:
|
||||
output = format(permutation)
|
||||
if output not in seen:
|
||||
seen.add(output)
|
||||
ans.append(output)
|
||||
|
||||
for x in sorted(ans):
|
||||
print(x)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
equation = input()
|
||||
|
||||
solve(equation)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue