前提・実現したいこと
AtCoder Beginner Contest 009のC問題が通らず、WAとなっている要因を知りたいです。
[AtCoder Beginner Contest 009 C - 辞書式順序ふたたび]
https://atcoder.jp/contests/abc009/tasks/abc009_3
発生している問題・エラーメッセージ
テストケースのうち11個のみACとなっております。
問題文における入力例は全て通っており、NGとなるケースも特定出来ていない状況です。
該当のソースコード
言語:Python3 (3.4.3)
Python
1import copy,sys 2input=sys.stdin.readline 3N,K=[int(x) for x in input().split()] 4S=list(input()) 5ans=[] 6cost=0 7remaining=copy.deepcopy(S) 8remaining.sort() 9 10def check(fix,w,S): 11 res=0 12 s=copy.deepcopy(S) 13 14 for i in range(len(fix)): 15 if fix[i]!=s[i]: 16 res+=1 17 s=s[len(fix):] 18 for wd in w: 19 if wd in s: 20 s.remove(wd) 21 else: 22 res+=1 23 return res 24 25for i in range(N): 26 for s in remaining: 27 t_sortedS=copy.deepcopy(remaining) 28 t_sortedS.remove(s) 29 c=check(ans+[s],t_sortedS,S) 30 if c>K: 31 continue 32 elif c<=K: 33 ans.append(s) 34 remaining.remove(s) 35 break 36 37print(''.join(ans))
(修正)
Python
1import copy,sys 2input=sys.stdin.readline 3N,K=[int(x) for x in input().split()] 4S=list(input()) 5ans=[] 6cost=0 7remaining=copy.deepcopy(S) 8remaining.sort() 9 10def check(fix,w,S): 11 res=0 12 s=copy.deepcopy(S) 13 14 for i in range(len(fix)): 15 if fix[i]!=s[i]: 16 res+=1 17 s=s[len(fix):] 18 seq=copy.deepcopy(w) 19 for wd in seq: 20 if wd in s: 21 s.remove(wd) 22 else: 23 res+=1 24 return res 25 26for i in range(N): 27 k='' 28 seq=copy.deepcopy(remaining) 29 for ii,s in enumerate(seq): 30 t_sortedS=copy.deepcopy(remaining) 31 t_sortedS.remove(s) 32 c=check(ans+[s],t_sortedS,S) 33 if c>K: 34 continue 35 elif c<=K: 36 ans.append(s) 37 k=s 38 break 39 40 remaining.remove(k) 41 42print(''.join(ans))
試したこと
問題文中の入力例の他、自身で作成した入力サンプルにて出力結果が問題無いことを確認。
補足情報(FW/ツールのバージョンなど)
プログラミング初心者故、大変見にくいコードかと思います。
質問内容と関係の無い観点でも、改善点などありましたらアドバイスを頂けると幸いです。