質問するログイン新規登録

回答編集履歴

1

コード修正

2018/04/11 06:01

投稿

8524ba23
8524ba23

スコア38350

answer CHANGED
@@ -6,21 +6,19 @@
6
6
  import itertools
7
7
 
8
8
  lines = ['あいう','あうえ','い','うい']
9
-
10
9
  lines = [''.join( sorted(line)) for line in lines]# 文字の並びは関係ないので最初に昇順に
11
10
 
12
11
  combs = {} # キー:各文字列に出現しうる文字の組み合わせ
13
- # 値 :どの文字列で出現したかを記録するマップ。要素数=出現した文字列の
12
+ # 値 :出現した文字列の位置の集合。要素数=出現数
14
13
 
15
14
  for idx, line in enumerate( lines):
16
15
  # N文字から 1...N文字でできる組み合わせ
17
16
  for n in range(len(line)):
18
17
  for i in itertools.combinations(line,n+1):
19
- s = ''.join( i)
20
- if s not in combs:
18
+ if i not in combs:
21
- combs[s] = {}
19
+ combs[i] = set()
22
- combs[s][idx] = 1 # この1という値は適当
20
+ combs[i].add(idx)
23
21
 
24
22
  for k,v in combs.items():
25
- print('({}) ->{}個'.format(k,len(v)))
23
+ print('{} ->{}個'.format(k,len(v)))
26
24
  ```