LoginSignup
0
1

More than 1 year has passed since last update.

A - ゾロ目数, A - We Love Golf, D - パターンマッチ

Last updated at Posted at 2021-03-07

##A - ゾロ目数

O(N)

python
import math
import heapq
import itertools
from functools import reduce
 
# main
def main():
    N = int(input())

    x = (N + 9 - 1) // 9
    y = N % 9
    if y == 0:
        y = 9
    
    res = ""
    for _ in range(0, x):
        res += str(y)
    
    print(res)

# エントリポイント
if __name__ == '__main__':
    main()

##A - We Love Golf

O(1)

python
import math
import heapq
import itertools
from functools import reduce
 
# main
def main():
    K = int(input())
    A, B = list(map(int, input().split()))

    x = A // K
    y = B // K

    ok = False
    if x < y:
        ok = True

    if A % K == 0:
        ok = True

    if ok:
        print("OK")
    else:
        print("NG")

# エントリポイント
if __name__ == '__main__':
    main()

##D - パターンマッチ

O(N)
使用可能文字を全探索します。

python
import math
import heapq
import itertools
from functools import reduce

def is_match(T, S):
    for i in range(0, len(S) - len(T) + 1):
        ok = True
        for j in range(0, len(T)):
            if S[i + j] != T[j] and T[j] != '.':
                ok = False
        if ok:
            return True
    return False

 
# main
def main():
    C = "abcdefghijklmnopqrstuvwxyz."
    S = str(input())

    M = []

    for T in C:
        if is_match(T, S):
            M.append(T)

    for c1 in C:
        for c2 in C:
            T = c1 + c2
            if is_match(T, S):
                M.append(T)

    for c1 in C:
        for c2 in C:
            for c3 in C:
                T = c1 + c2 + c3
                if is_match(T, S):
                    M.append(T)

    print(len(M))

# エントリポイント
if __name__ == '__main__':
    main()

O(N*2^3)
パターンマッチする文字列を入力の最初の要素から作成していきます。
データ構造のsetを利用することで重複する要素をカウントせずに追加できます。
'.'を設定する所は、bit_searchメソッド内でbit全探索にて要素を作成します。

python
import math
import heapq
import itertools
from functools import reduce

st = set()

def bit_search(T):
    N = len(T) ** 2
    for i in range(0, N+1):
        t = T
        for j in range(0, len(T)):
            if (i & (1 << j)):
                t = t[:j] + '.' + t[j+1:]

        st.add(t)

 
# main
def main():
    S = str(input())

    for i in range(0, len(S)):
        bit_search(S[i:i+1])
        bit_search(S[i:i+2])
        bit_search(S[i:i+3])

    print(len(st))

# エントリポイント
if __name__ == '__main__':
    main()
0
1
1

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
0
1