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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

lightoj 1044 - Palindrome Partitioning(需要优化的区间dp)

發布時間:2023/12/1 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 lightoj 1044 - Palindrome Partitioning(需要优化的区间dp) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:http://lightoj.com/volume_showproblem.php?problem=1044

題意:求給出的字符串最少能分成多少串回文串。

一般會想到用區間dp暴力3個for但是這里的數據有1000,3個for肯定超時的。

但是這題只是判斷回文串有多少個所以可以先預處理一下[i,j]是不是回文,然后

就是簡單dp了

for(int i = 1 ; i <= len ; i++) {

? ? ? ? ? ? ans[i] = ans[i - 1] + 1;

? ? ? ? ? ? for(int j = i - 1 ; j >= 1 ; j--) {

? ? ? ? ? ? ? ? if(dp[j][i]) {

? ? ? ? ? ? ? ? ? ? ans[i] = min(ans[i] , ans[j - 1] + 1);//如果[i,j]是回文那么就是ans[j-1]+1

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

#include <iostream> #include <cstring> #include <cstdio> using namespace std; int dp[1010][1010] , ans[1010]; char s[1010]; int main() {int t , cnt = 0;scanf("%d" , &t);while(t--) {cnt++;scanf("%s" , s + 1);int len = strlen(s + 1);memset(dp , 0 , sizeof(dp));memset(ans , 0 , sizeof(ans));for(int k = 0 ; k <= len ; k++) {for(int l = 1 ; l <= len && l + k <= len ; l++) {int r = l + k;if(s[l] == s[r]) {if(k == 0 || k == 1) {dp[l][r] = 1;}else {if(dp[l + 1][r - 1]) {dp[l][r] = 1;}}}else {dp[l][r] = 0;}}}for(int i = 1 ; i <= len ; i++) {ans[i] = ans[i - 1] + 1;for(int j = i - 1 ; j >= 1 ; j--) {if(dp[j][i]) {ans[i] = min(ans[i] , ans[j - 1] + 1);}}}printf("Case %d: %d\n" , cnt , ans[len]);}return 0; }

轉載于:https://www.cnblogs.com/TnT2333333/p/6645796.html

總結

以上是生活随笔為你收集整理的lightoj 1044 - Palindrome Partitioning(需要优化的区间dp)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。