There are many ways to represent a string. The following syntax
(x1 , k1 )(x2 , k2 ) . . . (xl , kl )
defines the string

where xi is the ith string that has to repeat ki times. We call this representation a brief string because it can represent a very long string by using only relatively small amount of space. For example, (ab, 2)(a, 4) represents ababaaaa. If you are given a brief string, certainly you can quickly recover the string that it represents.
Conversely, if you are given an ordinary string, you can find many di?erent brief strings that represent it. We are interested in finding the shortest one. We define the length of a brief string (x1 , k1 )(x2 , k2 ) . . . (xl , kl ) to be |x1 | + |x2 | + ... + |xl |. That is, we only consider the total length of strings that has to be repeated and ignore all the numbers (as well as the parentheses and commas). The shortest brief string of an ordinary string is called a basis.
For example, both (a, 1)(ba, 3)(a, 3) and (ab, 3)(a, 4) represent the same string abababaaaa.
However, only the second one is its basis whose length is 3. In this problem, you need to find the length of a basis of an ordinary string.
The first line of input contains an integer indicating the number of test cases. For each test case, an ordinary string is given on a single line.
Output the length of the basis of the speci?ed ordinary string for each test case.
1.The alphabet contains the lowercase English letters.
2.The length of an ordinary string is between 1 and 10000.
3.There are at most 20 test cases.
#include "bits/stdc++.h"
using namespace std;
const int maxn = 1e4 + 100;
int n;
char s[maxn];
int Next[maxn];
int f[maxn];
void getnext(char str[], int l) {
for (int i = 2, j = 0; i <= l; i++) {
while (j > 0 && str[i] != str[j + 1]) j = Next[j];
if (str[i] == str[j + 1]) j++;
Next[i] = j;
}
}
int main() {
//freopen("input.txt", "r", stdin);
int N, now, temp;
scanf("%d", &N);
while (N--) {
scanf("%s", s + 1);
n = strlen(s + 1);
for (int i = 0; i <= n; i++)
f[i] = i;
for (int i = 1; i <= n; i++) {
getnext(s + i - 1, n - i + 1);
for (int j = i; j <= n; j++) {
now = j - i + 1;
if (now % (now - Next[now]) == 0) {
f[j] = min(f[j], f[i - 1] + now - Next[now]);
}
}
}
printf("%d\n", f[n]);
}
return 0;
}