def valid(s): diff = 0 for e in s: if e == "L": diff += 1 else: diff -= 1 if diff < 0: return False return diff == 0 def solve(s): results = set() for mask in range(1, 1 << len(s)): sub_s = "" for i in range(len(s)): if mask & (1 << i) > 0: sub_s += s[i] if valid(sub_s): results.add(sub_s) return len(results) n = int(input()) data = [input() for _ in range(n)] for s1 in data: for s2 in data: print(solve(s1 + s2), end = " ") print()

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | def valid(s): diff = 0 for e in s: if e == "L": diff += 1 else: diff -= 1 if diff < 0: return False return diff == 0 def solve(s): results = set() for mask in range(1, 1 << len(s)): sub_s = "" for i in range(len(s)): if mask & (1 << i) > 0: sub_s += s[i] if valid(sub_s): results.add(sub_s) return len(results) n = int(input()) data = [input() for _ in range(n)] for s1 in data: for s2 in data: print(solve(s1 + s2), end = " ") print() |