Follow test instructions DP...DP (l, r) = min{DP (l, x) + DP (x+1, R), folding (L, R)}
Folding (L, R) I am directly enumerating the length and then hashing the hash:
--------------------------------------------------------------
#include <bits/stdc++.h>using namespace std;typedef unsigned long long ull;const int MAXN = 109;const int p = 101; Char S[MAXN];int D[MAXN][MAXN], N;ull H[MAXN], K[MAXN];bool Check (int l1, int r1, int l2, int r2) {return H[L1]-H[R1 + 1] * k[r1-l1 + 1] = = H[l2]-H[R2 + 1] * k[r2-l2 + 1];}int cnt (int x) {int ret = 0;For (; x; x/=) ret++;return ret + 2;}int dp (int l, int r) {int &t = d[l][r];if (t! =-1) return t;int len = r-l + 1;t = len;for (int i = LEN/2; i; i--) if (len% i = = 0 && check (l, r-i, L + i, r))t = min (t, CNT (LEN/I) + DP (L, L + i-1));for (int i = l; i < R; i++)t = min (t, DP (L, i) + DP (i + 1, R));return t;}int main () {scanf ("%s", s); N = strlen (S);k[0] = 1; H[n] = 0;for (int i = 1; I <= N; i++) k[i] = k[i-1] * p;for (int i = N; i--;) h[i] = h[i + 1] * p + s[i];memset (d,-1, sizeof d);for (int i = 0; i < N; i++) d[i][i] = 1;printf ("%d\n", DP (0, N-1));return 0;}
--------------------------------------------------------------
1090: [SCOI2003] string folding time limit: ten Sec Memory Limit: 162 MB
Submit: 826 Solved: 533
[Submit] [Status] [Discuss] Description
The definition of folding is as follows: 1. A string can be seen as its own folding. Remember it as s? S 2. X (S) is the folding of the string of X (x>1) s connected together. Remember as X (S)? SSSS ... s (x s). 3. What if a? A ', B? B ', then AB? A ' B ' For example, because 3 (a) = AAA, 2 (B) = BB, so 3 (a) C2 (b)? AAACBB, and 2 (3 (A) C) 2 (B)? AAACAAACBB gives a string to the shortest fold. For example, the shortest folding of the AAAAAAAAAABABABCCD is: 9 (A) 3 (AB) CCD.
Input
Only one line, the string s, guarantees a length of not more than 100.
Output
Only one line, that is, the shortest folding length.
Sample InputNeercyesyesyesneercyesyesyesSample Output -HINT
One of the shortest folds is: 2 (NEERC3 (YES))
Source
Bzoj 1090: [SCOI2003] String folding (interval dp)