1169D - Good Triple - CodeForces Solution


brute force *1900

Please click on ads to support us..

Python Code:

s = input()
n = len(s)
v = [n] * (n+1)
ans = 0
for i in range(n-1, -1,-1):
    v[i] = v[i + 1]
    k = 1
    while i + 2 * k < v[i]:
        if (s[i] == s[i + k] and s[i + k] == s[i + 2 * k]):
            v[i] = i + 2 * k
        k += 1
    ans += n - v[i]
print(ans)

C++ Code:

/*
Problem: 1169D
Date: 22-02-2024 04:57 AM
*/


#include <bits/stdc++.h>

#define FOR(i, n) for(int i = 0; i < n; i++)
#define FORK(i, k, n) for(int i = k; i < n; i++)
#define FORr(i, n) for(int i = n - 1; i >= 0; i--)
#define FORKr(i, k, n) for(int i = n - 1; i >= k; i--)
#define PRINT(a, b) copy((a), (b), ostream_iterator<int>(cout, " "))
#define all(a) a.begin(), a.end()
#define in(a, b) ((b).find(a) != (b).end())
#define sz(a) ((int) (a).size())
#define Mp make_pair
#define PII pair<int, int>
#define ll long long
#define VI vector<int>

using namespace std;

string s;
int n;

int main() {
    std::ios_base::sync_with_stdio(false);
    cin >> s;
    n = s.length();
    int l = 0;
    ll sum = 0;
    for(int i = 0; i < n; i++) {
        for(int j = 2; j < 10 && i + j < n; j++) {
            for(int k = 1; j - 2 * k >= 0 && k < 10; k++) {
                if(s[i + j] == s[i + j - k] && s[i + j] == s[i + j - 2 * k]) {
                    sum += (i - l + 1) * (n - (i + j));
                    l = i + 1;
                    break;
                }
            }
        }
    }
    cout << sum << endl;
}


Comments

Submit
0 Comments
More Questions

677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas
32B - Borze
1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math
832A - Sasha and Sticks
292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home
617A - Elephant
48A - Rock-paper-scissors
294A - Shaass and Oskols
1213A - Chips Moving
490A - Team Olympiad
233A - Perfect Permutation
1360A - Minimal Square
467A - George and Accommodation
893C - Rumor
227B - Effective Approach
1534B - Histogram Ugliness
1611B - Team Composition Programmers and Mathematicians
110A - Nearly Lucky Number
1220B - Multiplication Table
1644A - Doors and Keys