hihocoder 1320 压缩字符串(字符串+dp)
生活随笔
收集整理的這篇文章主要介紹了
hihocoder 1320 压缩字符串(字符串+dp)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題解:
其實就是對應(yīng)三種dp的轉(zhuǎn)移方式
1、拼接類型
dp[i][j] = dp[i][c] + dp[c][j]
2、不變類型
dp[i][j] = j-i+1
3、重復(fù)類型(必須滿足有k個循環(huán)節(jié))
dp[i][j] = width(k) + 2 + dp[i][i+L-1]
?
直接記憶化搜索即可,復(fù)雜度n^3logn(枚舉循環(huán)節(jié)近似為logn)
?
#include <iostream> #include <cstdio> #include <cstring> using namespace std; char S[110]; int dp[110][110];int width(int x){int l = 0;while(x) { l++; x /= 10; }return l; }int dfs(int i, int j){if(i > j) return 0;if(dp[i][j] < 100) return dp[i][j];int L = j-i+1;if(L == 1) return 1;dp[i][j] = L;for(int c = i; c < j; c++) dp[i][j] = min(dp[i][j], dfs(i, c) + dfs(c+1, j));for(int k = 2; k <= L; k++){if(L % k != 0) continue;int l = L/k, f = 0;for(int t = 0; t < k-1 && !f; t++){for(int c = 0; c < l && !f; c++)if(S[i+l*t+c] != S[i+l*(t+1)+c]) f = 1;}if(!f) dp[i][j] = min(dp[i][j], width(k)+2+dfs(i, i+l-1));}return dp[i][j]; }int main() {int T;cin>>T;while(T--){cin>>S;int n = strlen(S);memset(dp, 1, sizeof(dp));cout<<dfs(0, n-1)<<endl;}return 0; }?
轉(zhuǎn)載于:https://www.cnblogs.com/Saurus/p/7374744.html
《新程序員》:云原生和全面數(shù)字化實踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的hihocoder 1320 压缩字符串(字符串+dp)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++primer 10.6节练习
- 下一篇: [SCOI2005]扫雷