UVA1351 ----- String Compression ----- interval DP (memory-based search implementation)

Source: Internet
Author: User

According to his idea, the algorithm is self-implemented to give a string, which can be abbreviated to k (S) form for the same continuous part, and S is a string, k indicates that there are consecutive identical S. For example, abgogogogo can be abbreviated as ab4 (go ). you can also nest abbreviations, such as "nowletsgogogoletsgogogo", abbreviated as "now2 (lets3 (go)". idea: a range of dp, but this question is not really f (I, j) represents the string I ~ Then f (I, j) = min {f (I, k) + f (k + 1, j ), I <= k <j}, min {digitNum (k) + f [l] [l + k-1] + 2, if the string can be composed of the first k strings,} digitNum (k) indicates the number of digits of the number k to determine the interval (I, j) whether there are consecutive strings consisting of k consecutive strings, it is easy to use the O (n) Time to determine the interval DP to search by memory, you don't have to think about iterative writing, so you can use the code for memorizing search to write the interval DP in the future: # include <cstdio> # include <cstring> # include <iostream> # include <algorithm> # include <string. h> using namespace std; const int maxn = 300; int dp [maxn] [maxn]; char s [maxn]; const Int INF = 0x3f3f3f; bool check (int l, int r, int k) {int I; int len = r-l + 1; I = 0; while (I <k) {int p; for (p = 1; l + p * k + I <= r; p ++) {if (s [l + I]! = S [l + p * k + I]) return false;} I ++;} return true;} int min (int a, int B) {return a <B? A: B;} int digitnum (int k) {int len = 0; while (k> 0) {len ++; k/= 10;} return len ;} int DP (int l, int r) {if (dp [l] [r]! =-1) return dp [l] [r]; int len = r-l + 1; int d; dp [l] [r] = INF; for (int k = l; k <r; k ++) dp [l] [r] = min (dp [l] [r], DP (l, k) + DP (k + 1, r); for (d = 1; d <= len/2; d ++) {if (len % d! = 0) continue; if (check (l, r, d) {dp [l] [r] = min (dp [l] [r], digitnum (len/d) + DP (l, l + D-1) + 2);} return dp [l] [r];} int main () {int t; scanf ("% d", & t); while (t --) {scanf ("% s", s); memset (dp,-1, sizeof (dp )); int len = strlen (s); int I; for (I = 0; I <len; I ++) dp [I] [I] = 1; cout <DP (0, len-1) <endl;} return 0 ;}

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.