-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabc268_d.py
More file actions
246 lines (201 loc) · 6.93 KB
/
abc268_d.py
File metadata and controls
246 lines (201 loc) · 6.93 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
###############################################
###############################################
###############################################
###############################################
###############################################
###############################################
###############################################
###############################################
###############################################
###############################################
[my WA]
N,M=map(int,input().split())
S=[]
t=N-1
for i in range(N):
s=str(input())
S+=[s]
t+=len(s)
T=set()
for i in range(M):
T.add(str(input()))
if t>16 or t<3:
print(-1)
exit()
from itertools import product,permutations,combinations,accumulate
C=list(permutations(S))
for c in C:
X='_'.join(list(c))
if X not in T:
print(X)
exit()
print(-1)
###############################################
###############################################
###############################################
[ds]
def solve():
import sys
from itertools import permutations
input = sys.stdin.read
data = input().split()
N = int(data[0])
M = int(data[1])
S = []
idx = 2
for i in range(N):
S.append(data[idx])
idx += 1
T = set()
for i in range(M):
T.add(data[idx])
idx += 1
# 必要な文字数の計算
total_length = sum(len(s) for s in S)
min_length = total_length + (N - 1) # アンダースコアが最低1つずつ
max_length = 16
if min_length > max_length:
print(-1)
return
# 使用可能なアンダースコアの総数
available_underscores = max_length - total_length
# アンダースコアを(N-1)個の隙間に分配する全ての組み合わせを生成
for perm in permutations(S):
# 隙間の数
gaps = N - 1
# 各隙間に割り当てるアンダースコアの数を全探索
# 各隙間に最低1つ、合計でavailable_underscores以下のアンダースコア
from itertools import product
# 各隙間のアンダースコア数を表すリストの生成
for underscores in product(range(1, available_underscores - gaps + 2), repeat=gaps):
if sum(underscores) > available_underscores:
continue
# 文字列を構築
result = perm[0]
for i in range(gaps):
result += '_' * underscores[i] + perm[i + 1]
# 長さチェック
if len(result) < 3 or len(result) > 16:
continue
# Tに含まれていないかチェック
if result not in T:
print(result)
return
print(-1)
if __name__ == "__main__":
solve()
###############################################
[ds mod1]
from itertools import permutations
N,M=map(int,input().split())
S = []
idx = 2
for i in range(N):
S.append(input())
idx += 1
T = set()
for i in range(M):
T.add(input())
idx += 1
# 必要な文字数の計算
total_length = sum(len(s) for s in S)
min_length = total_length + (N - 1) # アンダースコアが最低1つずつ
max_length = 16
if min_length > max_length:
print(-1)
# 使用可能なアンダースコアの総数
available_underscores = max_length - total_length
# アンダースコアを(N-1)個の隙間に分配する全ての組み合わせを生成
for perm in permutations(S):
# 隙間の数
gaps = N - 1
# 各隙間に割り当てるアンダースコアの数を全探索
# 各隙間に最低1つ、合計でavailable_underscores以下のアンダースコア
from itertools import product
# 各隙間のアンダースコア数を表すリストの生成
for underscores in product(range(1, available_underscores - gaps + 2), repeat=gaps):
if sum(underscores) > available_underscores:
continue
# 文字列を構築
result = perm[0]
for i in range(gaps):
result += '_' * underscores[i] + perm[i + 1]
# 長さチェック
if len(result) < 3 or len(result) > 16:
continue
# Tに含まれていないかチェック
if result not in T:
print(result)
exit()
print(-1)
###############################################
[ds mod2]完全理解
from itertools import product,permutations,combinations,accumulate
N,M=map(int,input().split())
S = []
for i in range(N):
S.append(input())
T = set()
for i in range(M):
T.add(input())
tot_len = sum(len(s) for s in S)#文字合計、usなし
min_len = tot_len + (N - 1)#文字合計、us入り
max_len = 16#上限
if min_len > max_len:
print(-1)
exit()
avail_us = max_len - tot_len
for perm in permutations(S):#各文字列順列について
gaps = N - 1
for us in product(range(1, avail_us - gaps + 2), repeat=gaps):#us,各隙間に置くus数
if sum(us) > avail_us:
continue
result = perm[0]#文字列、us結合
for i in range(gaps):
result += '_' * us[i] + perm[i + 1]
if len(result) < 3 or len(result) > 16:
continue
if result not in T:
print(result)
exit()
print(-1)
###############################################
[cgpt]
def solve():
import sys
from itertools import permutations, product
n, m = map(int, sys.stdin.readline().split())
s_list = [sys.stdin.readline().strip() for _ in range(n)]
banned = {sys.stdin.readline().strip() for _ in range(m)}
base_len = sum(len(s) for s in s_list)
# 最低限のアンダースコアを入れた長さ
if base_len + n - 1 > 16:
print(-1)
return
max_us = 16 - base_len
gap_count = n - 1
# gaps に入れるアンダースコア数の列を事前生成
# 各隙間に必ず1個以上必要なので、その分を引いて計算する
for order in permutations(s_list):
for extra in product(range(max_us - gap_count + 2), repeat=gap_count):
# まず全ての隙間に最低1個ずつ入れて、さらに extra 分追加
if sum(extra) > max_us - gap_count:
continue
parts = [order[0]]
for i, add in enumerate(extra):
parts.append('_' * (1 + add))
parts.append(order[i + 1])
candidate = ''.join(parts)
if 3 <= len(candidate) <= 16 and candidate not in banned:
print(candidate)
return
print(-1)
if __name__ == "__main__":
solve()
###############################################
###############################################
###############################################
for perm in permutations(S):
gaps = N - 1
for underscores in product(range(1, available_underscores - gaps + 2), repeat=gaps):
###############################################