#include <iostream> #include <string> #include <algorithm> using namespace std; string f(const string& str, int cur, int len, int minLen) { int m = 1; string s = str.substr(cur, len); //cout << s << endl; for (int i = len + cur; i <= str.size()-len; i += len) { string tmp = str.substr(i, len); //cout << tmp << endl; if (s != tmp) break; ++m; } int ret = str.size() - m*len + len + 3; if (ret < minLen) { string tt(str.begin(), str.begin() + cur); tt += to_string(m); tt += '['; tt += s; tt += ']'; string tmp(str.begin() + cur + m*len, str.end()); tt += tmp; return tt; } else return ""; } int main() { string str; cin >> str; int len = str.size(); if (len < 5) { cout << len << endl; return 0; } int cur = 0; int ret = len; string newStr = str; while (true) { bool changed = false; for (int cur = 0; cur < str.size() - 4; ++cur) { //i位子串的长度 for (int i = 1; i <= str.size() / 2; ++i) { //cout << f(str, cur, i) << endl; string ff = f(str, cur, i, ret); if (ff != "") { ret = ff.size(); newStr = ff; changed = true; } } } str = newStr; if (!changed || str.size() < 5) break; } cout << ret << endl; }
点赞 5
牛客网
牛客企业服务