公式
D - A..B..C 解説 by en_translator
It is sufficient to exhaustively enumerate tuples \((i,j,k)\ (1\leq i<j<k\leq|S|)\) using triply-nested for
loop to check if each of them satisfies the condition, and count conforming ones.
For the details on implementation, please refer to the sample code (C++ and Python) below.
Bonus (worth ABC-C): solve it in \(O(|S|^2)\) time.
Bonus+ (worth ABC-F): solve it in \(O(|S|\log^2 |S|)\) time.
Sample code (C++):
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int n = s.size();
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
for (int k = j + 1; k < n; k++) {
if (j - i == k - j and s[i] == 'A' and s[j] == 'B' and s[k] == 'C') ++ans;
}
}
}
cout << ans << endl;
}
Sample code (Python):
s = input()
n = len(s)
ans = 0
for i in range(n):
for j in range(i + 1, n):
for k in range(j + 1, n):
if j - i == k - j and s[i] == 'A' and s[j] == 'B' and s[k] == 'C':
ans += 1
print(ans)
投稿日時:
最終更新: