区间DP UVA 1351 String Compression

时间:2024-01-17 23:07:08

题目传送门

 /*
题意:给一个字符串,连续相同的段落可以合并,gogogo->3(go),问最小表示的长度
区间DP:dp[i][j]表示[i,j]的区间最小表示长度,那么dp[i][j] = min (dp[j][k] + dp[k+1][i+j-1]),
digit (i / k) + dp[j][j+k-1] + 2)后者表示可以压缩成k长度连续相同的字符串
4.5  详细解释 */
/************************************************
* Author :Running_Time
* Created Time :2015-8-12 15:28:11
* File Name :UVA_1351.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = 2e2 + ;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + ;
char str[MAXN];
int dp[MAXN][MAXN]; bool check(int l, int r, int k) {
int i = ;
while (i < k) {
for (int p=; l+p*k+i<=r; ++p) {
if (str[l+i] != str[l+p*k+i]) return false;
}
++i;
}
return true;
} int digit(int x) {
int ret = ;
while (x) {
ret++; x /= ;
}
return ret;
} int main(void) { //UVA 1351 String Compression
int T; scanf ("%d", &T);
while (T--) {
scanf ("%s", str + );
int len = strlen (str + );
for (int i=; i<=len; ++i) dp[i][i] = ;
for (int i=; i<=len; ++i) {
for (int j=; j+i-<=len; ++j) {
dp[j][j+i-] = INF;
int &x = dp[j][j+i-];
for (int k=j; k<i+j-; ++k) {
x = min (x, dp[j][k] + dp[k+][i+j-]);
}
for (int k=; k<=i/; ++k) {
if (i % k != ) continue;
if (check (j, i + j - , k)) {
x = min (x, digit (i / k) + dp[j][j+k-] + );
}
}
}
} printf ("%d\n", dp[][len]);
} return ;
}