Click Here to go back to the homepage.

Red Rover Solution:


#include <bits/stdc++.h>

using namespace std;

int main(){
    // #ifndef TESTING
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    // #endif

    string instruction;
    cin >> instruction;
    map<string, pair<int, int>> counts;
    int maxReduction = INT_MIN;

    for(int i = 0; i < instruction.length(); i++){
        for(int len = 2; len <= instruction.length() - i && len < instruction.length() / 2; len++){
            string segment = instruction.substr(i, len);
            if(!counts.insert({segment, {-1, i + len}}).second){
                auto it = counts.find(segment);
                if(it->second.second > i){
                    continue;
                }
                it->second.first += len - 1;
                it->second.second = i + len;
                if(it->second.first > maxReduction){
                    maxReduction = it->second.first;
                }
            }
        }
    }
    if(maxReduction == INT_MIN){
        cout << instruction.length() << endl;
    } else {
        cout << instruction.length() - maxReduction << endl;
    }
    return 0;
}