1466C - Canine poetry - CodeForces Solution


dp greedy strings *1300

Please click on ads to support us..

Python Code:

t = int(input())
for _ in range(t):
    s = list(input())
    c = 0
    for i in range(1, len(s)):
        if i >= 2 and s[i] == s[i - 2] or s[i] == s[i - 1]:
            c += 1
            s[i] = '@'
    print(c)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define DIVYA                         \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define ll long long
#define cy cout << "YES\n"
#define cn cout << "NO\n"
typedef vector<ll> vll;
#define pb push_back
#define inf 1000000000000000005
const ll mod = 1e9 + 7;
ll inv(ll i)
{
    if (i == 1)
        return 1;
    return (mod - ((mod / i) * inv(mod % i)) % mod) % mod;
}
ll mod_mul(ll a, ll b)
{
    a = a % mod;
    b = b % mod;
    return (((a * b) % mod) + mod) % mod;
}
ll mod_add(ll a, ll b)
{
    a = a % mod;
    b = b % mod;
    return (((a + b) % mod) + mod) % mod;
}
ll ceil_div(ll a, ll b) { return a % b == 0 ? a / b : a / b + 1; }
ll bin_expo(int a, int b, int m)
{
    int res = 1;
    while (b > 0)
    {
        if (b & 1)
            res = (res * a) % m;
        a = (a * a) % m;
        b = b >> 1;
    }
    return res;
}
ll mod_inv(int a, int b) { return bin_expo(a, b - 2, b); }
//****************************Template Ends*******************************//

int main()
{
    DIVYA;
    ll t, n,m, i, j, ans,temp, sum,pos = 0,check = 1,x,y,cnt = 0;
    string s;
    t = 1;
    cin >> t;
    while (t--)
    {
         ans = temp = sum = 0;
         cin >> s;
         n = s.size();
         for(int i = 0; i < n; i++)
         {
             if(s[i] == s[i+1] and s[i] != '#' and i + 1 < n)
             {
                 ans++;
                 s[i+1] = '#';
             }
             if(s[i+2] == s[i] and s[i] != '#' and i + 2 < n)
             {
                 ans++;
                 s[i+2] = '#';
             }
         }
         cout << ans << "\n";
         
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1703E - Mirror Grid
1042A - Benches
1676B - Equal Candies
1705B - Mark the Dust Sweeper
1711A - Perfect Permutation
1701B - Permutation
1692A - Marathon
1066A - Vova and Train
169B - Replacing Digits
171D - Broken checker
380C - Sereja and Brackets
1281B - Azamon Web Services
1702A - Round Down the Price
1681C - Double Sort
12A - Super Agent
1709A - Three Doors
1680C - Binary String
1684B - Z mod X = C
1003A - Polycarp's Pockets
1691B - Shoe Shuffling
1706A - Another String Minimization Problem
1695B - Circle Game
1702B - Polycarp Writes a String from Memory
1701A - Grass Field
489C - Given Length and Sum of Digits
886B - Vlad and Cafes
915A - Garden
356A - Knight Tournament
1330A - Dreamoon and Ranking Collection
1692B - All Distinct