LoginSignup
1
1

More than 5 years have passed since last update.

AtCoder Regular Contest #002 のC問題

Posted at

hi!
Come to think of it, no one has written in English, but I wonder if scolded?

と英語で書き続けようかとも思ったんですが無理すぎたので諦め。
http://arc002.contest.atcoder.jp/tasks/arc002_3
ARC #002はもう去年の5月なので結構な昔なのです。なのです。ただ、今夜解き直したら当時の回答が謎の書き方だった割に処理速度負けてて恥ずかしくて死んでしまいたくなりました。

新しい回答.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys
import io
import re
import math

N=int(raw_input())
c=raw_input()
chk=['AA','AB','AX','AY',
     'BA','BB','BX','BY',
     'XA','XB','XX','XY',
     'YA','YB','YX','YY']
cnt=1000
for i in chk:
    c2=c.replace(i,'L')
    for j in chk:
        c3=c2.replace(j,'R')
        cnt=min(cnt,len(c3))
print cnt

chkのリストの中からもれなくLやRに置換しようとしているので2重ループの1周目はiでAAをLに置換したのにjでもAAを探して次にAが残っているかにかかわらずAB、AXと探しに行きます。時間やメモリ制限が緩いおかげでAC通りますね。はい。

では下が1年半前ころに書いたものです。

昔の回答.py
import sys

M=raw_input()
N=raw_input()
x=[]

N2=N.replace('AA','L');N3=N2.replace('AB','R');x.append(len(N3))
N2=N.replace('AA','L');N3=N2.replace('AX','R');x.append(len(N3))
N2=N.replace('AA','L');N3=N2.replace('AY','R');x.append(len(N3))
N2=N.replace('AA','L');N3=N2.replace('BA','R');x.append(len(N3))
N2=N.replace('AA','L');N3=N2.replace('BB','R');x.append(len(N3))
N2=N.replace('AA','L');N3=N2.replace('BX','R');x.append(len(N3))
#
#ここの間も本気で全組み合わせ列挙してます。本当に。
#
N2=N.replace('YA','L');N3=N2.replace('YY','R');x.append(len(N3))
N2=N.replace('YB','L');N3=N2.replace('YB','R');x.append(len(N3))
N2=N.replace('YB','L');N3=N2.replace('YX','R');x.append(len(N3))
N2=N.replace('YB','L');N3=N2.replace('YY','R');x.append(len(N3))
N2=N.replace('YX','L');N3=N2.replace('YX','R');x.append(len(N3))
N2=N.replace('YX','L');N3=N2.replace('YY','R');x.append(len(N3))
N2=N.replace('YY','L');N3=N2.replace('YY','R');x.append(len(N3))

x.sort()
print x[0]

手書きで全組み合わせ列挙したのにLで置換した組合せを平気でRでも探しに行ってますね。何をこんなに頑張って書いたんだろうか

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