LoginSignup
1
1

More than 5 years have passed since last update.

ARC #008 のB問題

Last updated at Posted at 2013-12-16

はい。
http://arc008.contest.atcoder.jp/

大まかな方針
英字キットを先頭から末尾まで見る。名前の中の文字と一致したらそれを削除。
英字キットを使い切ったのに開始前と名前の長さが変わってなかったら永遠に作れないので-1
長さが減ってればカウントを1上げてもう一度英字キットを先頭から〜

op.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys
import io
import re
import math

(n,m) = map(int, raw_input().split())
#o = map(str, raw_input().split())
name = list(raw_input())
#p = map(str, raw_input().split())
kit = list(raw_input())
ans=0
zen=0
stp=len(name)
while 1:
    for i in range(len(kit)):
        if kit[i] in name: name.remove(kit[i])
    ans+=1
# != の条件は1周目でnameが0になる場合の対策
    if stp==len(name) or (zen==len(name) and len(name)!=0):
        ans = -1
        break
    elif len(name)==0:
        break
    zen=len(name)
print ans

見たことがないモジュールを使ってらっしゃる回答を見たのでメモ

めも.py
>>> from collections import Counter
>>> a=Counter(raw_input())
abcdefgh
>>> a
Counter({'a': 1, 'c': 1, 'b': 1, 'e': 1, 'd': 1, 'g': 1, 'f': 1, 'h': 1})

>>> a['a']
1
## [' '] の中に要素名を入れると要素数を出せるっぽい

type(a) #<class 'collections.Counter'>
1
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1