Official
B - 行儀のよい列 / Polite Queue Editorial
by
B - 行儀のよい列 / Polite Queue Editorial
by
kyopro_friends
問題文の指示通り各 \(i\) が「 \(S_i=\) C かつ \(S_{i+1}=\) ‘A’ 」を満たすかどうかを全て調べれば良いです。
実装例 (C++)
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
string s;
cin >> s;
int ans = 0;
for(int i=0; i<n-1; i++){
if(s[i] == 'C' && s[i+1] == 'A'){
ans++;
}
}
cout << ans << endl;
}
実装例 (Python)
N = int(input())
S = input()
ans = 0
for i in range(N-1):
if S[i] == 'C' and S[i+1] == 'A':
ans += 1
print(ans)
なおこの問題は「\(S\) は CA を何個含むか?」と言い換えることができるため、 python では count メソッドを用いることで簡単に実装することができます。
実装例 (Python)
N = int(input())
S = input()
print(S.count("CA"))
posted:
last update:
