-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpattern_matching.py
62 lines (52 loc) · 1.48 KB
/
pattern_matching.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def solve(n):
suffix = []
prefix = []
middle = []
multiple = []
current_suffix = ''
current_prefix = ''
impossible = False
for _ in range(n):
pattern = str(input())
if pattern.count('*') == 1:
if pattern.startswith('*'):
suffix.append(pattern)
elif pattern.endswith('*'):
prefix.append(pattern)
else:
middle.append(pattern)
else:
multiple.append(pattern)
if middle:
for mid in middle:
pre, suf = mid.split('*')
suf = '*' + suf
pre = pre + '*'
suffix.append(suf)
prefix.append(pre)
if suffix:
current_suffix = max(suffix, key=len)
current_suffix = current_suffix[1:]
for word in suffix:
if current_suffix.endswith(word[1:]):
pass
else:
impossible = True
if prefix:
current_prefix = max(prefix, key=len)
current_prefix = current_prefix[:-1]
for word in prefix:
if current_prefix.startswith(word[:-1]):
pass
else:
impossible = True
if impossible:
return '*'
else:
return current_prefix + current_suffix
if __name__ == '__main__':
t = int(input())
for case in range(1, t+1):
n = int(input())
result = solve(n)
print('Case #{}: {}'.format(case, result))