[初心者のメモ]　AtCoderの過去問精選10選をPythonで考えたメモ

[AtCoder に登録したら次にやること ～ これだけ解けば十分闘える！過去問精選 10 問 ～](https://qiita.com/drken/items/fd4e5e3630d0f5859067)
をプログラミング始めたての素人がPythonで考えるだけ

# 1問目

```python
a, b = map(int, input().split(" "))

if a*b % 2 == 0:
  print("Even")
else:
  print("Odd")
```

# 2問目

```python
s = str(input())

counter = 0
for i in range(len(s)):
    if s[i] == "1":
        counter += 1
    else:
        next

print(counter)
```

# 3問目

```python
N = int(input())
kokuban = list(map(int, input().split()))
 
counter = 0
roop_checker = True
while roop_checker == True:
    for i in range(N):
        if kokuban[i] % 2 == 1:
            roop_checker = False
        else:
            kokuban[i] = kokuban[i] / 2
    if roop_checker == True:
        counter += 1
    else:
        next
 
print(counter)
```

# 4問目

```python
a_500 = int(input())
b_100 = int(input())
c_50 = int(input())
x_total = int(input())

counter = 0
for i in range(a_500 + 1):
    for j in range(b_100 + 1):
        for k in range(c_50 + 1):
            if x_total == i*500 + j*100 + k*50:
                counter += 1
print(counter)
```

# 5問目
```python
N, a, b = map(int, input().split(" "))
 
ok_list = []
for i in range(N):
    N_now = i+1
    sum_tmp = 0
    for j in range(len(str(N_now))):
        sum_tmp += int(str(N_now)[j])
    
    if sum_tmp >= a and sum_tmp <= b:
        ok_list.append(N_now)

print(sum(ok_list))
```

# 6問目
↓ ソートについて参考にした記事
[Pythonでリストをソートするsortとsortedの違い](https://note.nkmk.me/python-list-sort-sorted/)

```python
N = int(input())
card_values = list(map(int, input().split(" ")))
 
card_values_sorted = sorted(card_values, reverse=True)

Alice_score = 0
Bob_score = 0
for i in range(N):
    if i%2 == 0:
        Alice_score += card_values_sorted[i]
    else:
        Bob_score += card_values_sorted[i]
 
print(Alice_score - Bob_score)
```

# 7問目

↓ 重複する値を削除するのに役立った記事
[Pythonでリスト（配列）から重複した要素を削除・抽出]
(https://note.nkmk.me/python-list-unique-duplicate/)

```python
N = int(input())
mochi_init = [int(input()) for i in range(N)]

mochi_set = set(mochi_init)
print(len(mochi_set))
```

# 8問目
↓ 多重ループを抜け出すのに役立った記事
[多重ループを一気に抜ける]
(https://qiita.com/airtoxin/items/ae1e82d65b8f3d1efbf2)

もっとスマートな書き方がありそう

```python
N, Yen_target = map(int, input().split(" "))

flag = False
for i in range(N+1):
    for j in range(N+1-i):
            if Yen_target == i*10000 + j*5000 + (N-i-j)*1000:
                print(i, j, N-i-j)
                flag = True
            else:
                continue
            if flag:
                break
    if flag:
        break
if flag == False:
    print(-1, -1, -1)
```

# 9問目(失敗例です。一部はACだけど、WA)

過去問精選10選にあったように, dreameraserをdreamer + aserにしてしまう。
（本当はdream + eraserでYES）

```python
S = str(input())
words = ["dream", "dreamer", "erase", "eraser"]

checker = True
while checker == True:
    if S == "":
        checker = False
    elif S[0] == "d":
        if S[0:7] == "dreamer":
            if len(S) >= 7:
                S = S[7:]
            else:
                checker = False
        elif S[0:5] == "dream":
            if len(S) >= 5:
                S = S[5:]
            else:
                checker = False
        else:
            continue
    elif S[0] == "e":
        if S[0:6] == "eraser":
            if len(S) >= 6:
                S = S[6:]
            else:
                checker = False
        elif S[0:5] == "erase":
            if len(S) >= 5:
                S = S[5:]
            else:
                checker = False
    else:
        checker = False

        
if S == "":
    print("YES")
else:
    print("NO")
```

# 問題9 (失敗例2)
後ろから削除するように改良してみた
正しく動いているみたいだけど、時間がかかりすぎのよう(TLE)

```python
S = str(input())

checker = True
while checker == True:
    if S == "":
        checker = False
    elif S[-1] == "r":
        if S[-7:] == "dreamer":
            if len(S) >= 7:
                S = S[:-7]
            else:
                checker = False
        elif S[-6:] == "eraser":
            if len(S) >= 6:
                S = S[:-6]
            else:
                checker = False
        else:
            continue
    elif S[-1] == "e":
        if S[-5:] == "erase":
            if len(S) >= 5:
                S = S[:-5]
            else:
                checker = False
    elif S[-1] == "m":
        if S[-5:] == "dream":
            if len(S) >= 5:
                S = S[:-5]
            else:
                checker = False
    else:
        checker = False

        
if S == "":
    print("YES")
else:
    print("NO")
```


# 9問目(正解例)
AtCoderは, つまったときには他の方の解答が見れるのが素晴らしい

## [他の方の解答1](https://atcoder.jp/contests/abc049/submissions/7215458)
正規表現を使いこなせるのはすごい

```python
import re
S = input()
if re.fullmatch('(dream|dreamer|erase|eraser)+', S):
  print('YES')
else:
  print('NO')
```

## [他の方解答2](https://atcoder.jp/contests/abc049/submissions/7192817)
後ろから文字列を検索するrfindを使う
(参考: [Python Tips：文字列を検索したい](https://www.lifewithpython.com/2015/04/python-search-string-pattern.html))

```python
# ABC 49 C

def resolve():
    s = input()
    while (len(s) != 0):
        if s.rfind('eraser') == len(s) - len('eraser') and len(s) - len('eraser') >= 0:
            s = s[:len(s) - len('eraser')]
        elif s.rfind('erase') == len(s) - len('erase') and len(s) - len('erase') >= 0:
            s = s[:len(s) - len('erase')]
        elif s.rfind('dreamer') == len(s) - len('dreamer') and len(s) - len('dreamer') >= 0:
            s = s[:len(s) - len('dreamer')]
        elif s.rfind('dream') == len(s) - len('dream') and len(s) - len('dream') >= 0:
            s = s[:len(s) - len('dream')]
        else:
            print('NO')
            return

    if len(s) == 0:
        print('YES')
    else:
        print("NO")


if __name__ == "__main__":
    resolve()

```


# 10問目

```python
N = int(input())
txy_init = [[0, 0, 0]]
txy = txy_init + [[int(i) for i in input().split(" ")] for i in range(N)]
t, x, y = 0, 1, 2

checker = True
for i in range(N):
    dt = txy[i+1][t] - txy[i][t]
    dxy = abs(txy[i+1][x] - txy[i][x]) + abs(txy[i+1][y] - txy[i][y])
    
    if dt < dxy:
        checker = False
    else:
        if (dt - dxy) % 2 == 0:
            continue
        else:
            checker = False
    
    if checker:
        continue
    else:
        break

if checker:
    print("Yes")
else:
    print("No")
```