[Leetcode 471] Encode String with Shortest Length

Source: Internet
Author: User
Tags encode string

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.