日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

NYOJ 1067 Compress String(区间dp)

發(fā)布時(shí)間:2025/3/16 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 NYOJ 1067 Compress String(区间dp) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Compress String

時(shí)間限制:2000?ms ?|? 內(nèi)存限制:65535?KB 難度:3 描述
One day,a beautiful girl ask LYH to help her complete a complicated task—using a new compression method similar to Run Length Encoding(RLE) compress a string.Though the task is difficult, LYH is glad to help her. The compress rules of the new method is as follows: if a substring S is repeated k times, replace k copies of S by k(S). For example,?letsgogogo?is compressed into?lets3(go). The length of?letsgogogo?is 10, and the length of?lets3(go)?is 9. In general, the length of k(S) is (number of digits in k ) + (length of S) + 2. For example, the length of?123(abc)?is 8. It is also possible that substring?S is a compressed string. For example?nowletsgogogoletsgogogoandrunrunrun?could be compressed as?now2(lets3(go))and3(run). In order to make the girl happy, LYH solved the task in a short time. Can you solve it? 輸入
Thera are multiple test cases.
Each test case contains a string, the length of the string is no more than 200, all the character is lower case alphabet.
輸出
For each test case, print the length of the shortest compressed string.
樣例輸入
ababcd letsgogogo nowletsgogogoletsgogogoandrunrunrun
樣例輸出
6 9 24

題意:給出一個(gè)長(zhǎng)度不超過(guò)200的字符串,把這個(gè)字符串按照一定規(guī)則壓縮,即可以把幾個(gè)連續(xù)的相同子串壓縮成一個(gè)串,例如可以把letsgogogo壓縮為lets3(go),壓縮后的子串如果還可以繼續(xù)壓縮,則可以繼續(xù)壓縮,如可以將nowletsgogogoletsgogogoandrunrunrun壓縮為now2(lets3(go))and3(run)。問(wèn)經(jīng)過(guò)壓縮后這個(gè)字符串的最短長(zhǎng)度是多少。

分析:?區(qū)間DP,dp[i][j]表示從i到j(luò)的字符串表示的最短長(zhǎng)度。

? ? ? ? ? dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j])。
? ? ? ? ? 然后去判斷當(dāng)前子串能不能壓縮,即是否由重復(fù)字符串組成,判斷時(shí)只需暴力枚舉重復(fù)長(zhǎng)度,去判斷即可。
? ? ? ? ? 如果當(dāng)前子串可以壓縮,則dp[i][j] = min(dp[i][j], dp[i][i + len - 1] + 2 + digcount((j - i + 1) / len));,

? ? ? ? ? 注意如果是數(shù)字,要用數(shù)字的位數(shù)表示增加的個(gè)數(shù),而不是單純的增加1.

#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N = 210; #define INF 0x3fffffff char str[N]; int n, dp[N][N];int digit_cnt(int x) {int a = 0;while(x) {a++;x /= 10;}return a; }bool check(int l, int r, int len) {if((r - l + 1) % len) return false;for(int i = l; i < l + len; i++) {for(int j = i + len; j <= r; j += len)if(str[i] != str[j]) return false;}return true; }int get_ans() {int i, j, k;n = strlen(str+1);for(i = 1; i <= n; i++) dp[i][i] = 1;for(i = n - 1; i >= 1; i--) {for(j = i + 1; j <= n; j++) {dp[i][j] = INF;for(k = i; k < j; k++)dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j]);for(int len = 1; len <= j-i+1; len++) {if(check(i, j, len)) {dp[i][j] = min(dp[i][j], dp[i][i+len-1] + 2 + digit_cnt((j - i + 1) / len));}}}}return dp[1][n]; }int main() {while(~scanf("%s", str+1)) {printf("%d\n", get_ans());}return 0; }

總結(jié)

以上是生活随笔為你收集整理的NYOJ 1067 Compress String(区间dp)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。