ARC # 008 B problem

Yes. http://arc008.contest.atcoder.jp/

Rough policy View the alphabet kit from beginning to end. If it matches the letters in the name, delete it. Even though I've used up the alphabet kit, if the length of the name hasn't changed from before the start, I can't make it forever, so -1 If the length decreases, increase the count by 1 and start the alphabet kit again from the beginning ~

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
# !=The condition of is the countermeasure when the name becomes 0 in the first lap
    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

I saw the answer that you are using a module that you have never seen, so make a note

Memo.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
## [' ']It seems that you can get the number of elements by putting the element name in

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

Recommended Posts

ARC # 008 B problem
Probability problem
Python learning record (paiza: B problem: calculation drill)
Python learning record (paiza B problem: math drill)