YottaGin

Freedom is a responsible choice.

[Python] ABC019 B

2020/1/4 2020/1/4 プログラミング

問題

B – 高橋くんと文字列圧縮

回答

s = input()

s_compressed = ''
cnt = 1

for i, ch in enumerate(s):
    if i == 0:
        ch_pre = ch
        continue
    
    if ch == s[i-1]:
        cnt += 1
    else:
        s_compressed += ch_pre + str(cnt)
        ch_pre = ch
        cnt = 1
        
s_compressed += ch_pre + str(cnt)

print(s_compressed)

Atcoder, Python GinO

関連記事

[Python] 連結リストを逆順にする

Python で連結リストを逆順にします。 連結リストは以下の実装を使います。 単純に考えれば、空の連結リストを用意して値を先頭から挿入していくことで、逆順の連結リストを取得できます。 def reverse_linked_list(sel...

記事を読む

[Python] 2分木/binary tree

Pythonで2分木を実装します。 2分木 二分木(binary tree; 二進木、バイナリツリー)は、データ構造の1つである。根付き木構造の中で、あるノード(節点 node)が持つ子の数が高々2であるものをいう。典型的には2つの子はそれぞれ「左」「右」と呼ばれ...

記事を読む

[Python] ABC002 B

B - 罠 W = input() output = '' for w in W: if w not in 'aiueo': output += w print(output) メモ pythonでは、in を使うと、その文字が含まれる...

記事を読む

[Python] ABC013 B

問題 B - 錠 回答 前方向と後ろ方向に全探索します。 a = int(input()) b = int(input()) def next_digit(num): if num == 9: return 0 else...

記事を読む

[Python] コマンドプロンプトでtermcolorが文字化けする時

windows10のコマンドプロンプトでpythonのtermcolorで出力に色をつけようとすると、色ではなく文字化けが発生します。 termcolor は unix で使われる ANSIエスケープシーケンスというものを使うためで、これが windows のコマンドプ...

記事を読む


Public Domain YottaGin No Rights Reserved.