LoginSignup
111
119

More than 3 years have passed since last update.

【Python】競プロテンプレ【AtCoder】

Last updated at Posted at 2020-05-04

現在(2020/05/05)使用している
AtCoderなどの競プロ(Python用)のテンプレ!
よかったら使ってください〜
(万が一間違いがあったらすいません!)

競プロテンプレ

1行目は削除して提出することが多いと思うので、
(入力時で)必ず使用するsysとそれ以外のライブラリ
の2行に分けている!
また、input()よりsys.stdin.readline().rstrip()の方が明らかに早い!!!
参考記事
Pythonの知っておくと良い細かい処理速度の違い8個

(2020/05/17追記)
S()の位置をLS()の上に移動(見栄えがよくなかったため)

test.py
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
N = I()
A = [LI() for _ in range(N)]

※補足1〜ライブラリたちの主な使い道〜

(2020/05/17追記)
np.argmax()を追記

  • bisect
    • bisect()
  • collections
    • deque()
    • Counter()
    • most_common()
    • defaultdict()
  • copy
    • deepcopy()
  • heapq
    • heapify()
    • heappush()
    • heappop()
  • itertools
    • accumulate()
    • groupby()
    • product()重複あり
    • permutations()重複なし(順列)
    • combinations()重複なし(組み合わせ)
  • math
    • gcd()
    • ceil()
    • factorial()
  • numpy
    • fmax()
    • argmax()
    • vstack()
  • string
    • ascii_lowercase()
    • ascii_uppercase()
    • lower()
    • upper()
    • swapcase()

※補足2〜テンプレ使用時の注意点〜

(2020/05/11追記)
ABC167A
で以下のように文字受け取ると、エラーが出て焦った・・・

NG.py
import sys
def S(): return sys.stdin.readline().rstrip()
S = S()
T = S()
print('Yes' if S==T[:-1] else 'No')

T = S()
のところで、
Sは(直前で上書きされて)ただの文字列なのに、S()なんて使えないよ!とのエラー!
よくよく考えたら当たり前ですね・・・

こういう時は、以下のように工夫して対処しましょう〜

OK1.py
import sys
def S(): return sys.stdin.readline().rstrip()
S,T = S(),S()
print('Yes' if S==T[:-1] else 'No')

これならいける!というか普通にこっちの書き方でよかった・・・

OK2.py
import sys
def S(): return sys.stdin.readline().rstrip()
s = S()
t = S()
print('Yes' if s==t[:-1] else 'No')

変数を小文字(大文字のS以外)にする〜

おわり!

111
119
0

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
111
119