Trick: int r = (sub + sub). Find (Sub, 1); look for repeating pattern. Sub.size ()% r = = 0 must be established. Otherwise set R is the search result, always find a smaller R satisfies the condition.
1 classSolution {2 Public:3 stringEncodestrings) {4 intn =s.size ();5vector<vector<string>> DP (n +1, vector<string> (n +1));6 for(inti =0; I < n; i++) {7Dp[i][i +1].push_back (S[i]);8 }9 for(intK =2; K <= N; k++) {Ten for(inti =0, j = k; J <= N; i++, J + +) { OneDP[I][J] = dp[i][j-1] + dp[j-1][j]; A for(intm = i +1; M < J; m++) { - stringTMP = Dp[i][m] +Dp[m][j]; - if(Tmp.size () <dp[i][j].size ()) { theDP[I][J] =tmp; - } - } - intLen = J-i; + stringSub =S.substr (i, Len); - intR = (sub + sub). Find (Sub,1); + if(R <Len) { A stringTMP = to_string (LEN/R) +"["+ Dp[i][i + R] +"]"; at if(Tmp.size () <dp[i][j].size ()) { -DP[I][J] =tmp; - } - } - } - } in returndp[0][n]; - } to};
[Leetcode 471] Encode String with Shortest Length