443B - Kolya and Tandem Repeat - CodeForces Solution


brute force implementation strings *1500

Please click on ads to support us..

Python Code:

s = input() + 'X' * int(input())
for v in range(len(s) - len(s) % 2, 0, -2):
    for i in range(len(s) - v + 1):
        if all(s[j + v // 2] in ('X', s[j]) for j in range(i, i + v // 2)):
            print(v), exit()

C++ Code:

#include <array>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <sstream>

typedef long long ll;

using namespace std;

void solve() {
    string s;
    int k;

    cin >> s >> k;
    int n = s.size();

    if(k >= n){
        cout << (n+k)/2*2 << endl;
        return;
    }

    int ret = 2 * k;

    for(int i = n-k-1; i >= 0; i--){
        int max_h = (n + k - i) / 2;
        for(int h = 1; h <= max_h; h++){
            bool found = true;
            for(int j = 0; j < h; j++){
                if(i+j+h >= n) break;
                if(s[i+j] != s[i+j+h]){
                    found = false;
                    break;
                }
            }
            if(found){
                ret = max(ret, 2 * h);
            }
        }
    }
    cout << ret << endl;
    return;
}

int main(){
    // ios_base::sync_with_stdio(false);
    // cin.tie(NULL);
    int t = 1;
    // cin >> t;
    while(t--)
        solve();

    return 0;
}


Comments

Submit
0 Comments
More Questions

766B - Mahmoud and a Triangle
1618C - Paint the Array
469A - I Wanna Be the Guy
1294A - Collecting Coins
1227A - Math Problem
349A - Cinema Line
47A - Triangular numbers
1516B - AGAGA XOOORRR
1515A - Phoenix and Gold
1515B - Phoenix and Puzzle
155A - I_love_username
49A - Sleuth
1541A - Pretty Permutations
1632C - Strange Test
673A - Bear and Game
276A - Lunch Rush
1205A - Almost Equal
1020B - Badge
1353A - Most Unstable Array
770A - New Password
1646B - Quality vs Quantity
80A - Panoramix's Prediction
1354B - Ternary String
122B - Lucky Substring
266B - Queue at the School
1490A - Dense Array
1650B - DIV + MOD
1549B - Gregor and the Pawn Game
553A - Kyoya and Colored Balls
1364A - XXXXX