問題はこちら
3完
A
効率的な方法を探したい
S= input() for i in range(len(S)): if i != len(S)-1: print(S[i] + " ", end = "") else: print(S[i] , end = "")
B
もっと効率的な書き方があると思うが、Nが小さいので許してください
N= int(input()) A = list(map(int, input().split())) A_max = max(A) B = [] for a in A: if a != A_max: B.append(a) print(max(B))
C
文字が1種類で構成されている部分文字列の種類数を求めたい。
文字aが最大10回続けば、aによる部分文字列は10種類であるとわかる。
よって、各文字が最大何回続くかを求めてそれを足し合わせればよい。
最後の文字を辞書に追加することを忘れないように注意する必要がある。
N= int(input()) S = input() dic_S = {} # aが最大でいくつ続くか などをすべての文字について格納しておく変数 count_subst = S[0] # 続いている数をカウントする文字 n_cont = 1 # 何回続いているかを記録する dic_S[count_subst] = n_cont for i in range(1,N): if S[i] == count_subst: n_cont += 1 # 文字が連続するならカウントを増やす else: # 文字が連続しないなら、辞書に追加し新しい文字でカウントを始める # 辞書に追加 if count_subst in dic_S: dic_S[count_subst] = max(dic_S[count_subst], n_cont) else: dic_S[count_subst] = n_cont # #変数リセット count_subst = S[i] n_cont =1 # 最後の文字の処理 # 辞書に追加 if count_subst in dic_S: dic_S[count_subst] = max(dic_S[count_subst], n_cont) else: dic_S[count_subst] = n_cont # calc answer print(sum(dic_S.values()))
D
Winnerを全候補から探索していて、TLE、、、
N,M = list(map(int, input().split())) A = list(map(int, input().split())) get_vote_list = [ 0 for i in range(N+1)] # 候補者iが何票得ているか。候補者名とIndexを一致させる before_winner = 0 for i in range(M): voted_candidate = A[i] get_vote_list[A[i]] += 1 if get_vote_list[voted_candidate] > get_vote_list[before_winner]: # 得票数に差があれば、得票の多い人が勝ち winner = voted_candidate elif get_vote_list[voted_candidate] < get_vote_list[before_winner]: # 得票数に差があれば、得票の多い人が勝ち winner = before_winner else: # 得票数が同じなら、Indexの若い人が勝ち winner = min(voted_candidate, before_winner) print(winner) # 出力 before_winner = winner # before_winner更新
E
F