lightoj 1044 - Palindrome Partitioning DP优化DP
来源:互联网 发布:unity3d场景下载 编辑:程序博客网 时间:2024/06/06 03:48
给定一个串,问这个串可以最少拆分为几个回文子串。
一开始就想到了dp[i]代表i之前有多少个回文串,然后枚举j,区间[j,i]是否回文,如果是回文的话min(dp[i]=dp[j-1]+1,dp[i]).
但是n有1000,枚举i,j还要判断区间回文,复杂度n^3肯定要爆...
苦思冥想,其实预处理一下hw[i][j]判断i,j是否是个回文串。
之前做过回文串类的区间DP,一个两重循环就可以打表得出,复杂度n^2..
然后百度了其他人的做法,貌似最开始的思路可以过?..
#include<bits/stdc++.h>using namespace std;#define ll long long#define N 1005int hw[N][N];char s[N];int dp[N];int main(){ int t; scanf("%d",&t); for(int cas=1;cas<=t;cas++) { scanf("%s",s+1); int len=strlen(s+1); memset(hw,0,sizeof(hw)); memset(dp,0,sizeof(dp)); for(int i=1;i<=len;i++) { for(int l=1;l+i-1<=len;l++) { int r=l+i-1; if(s[l]==s[r]) hw[l][r]=l+1>r-1?1:hw[l+1][r-1]; else hw[l][r]=0; } } for(int i=1;i<=len;i++) { dp[i]=dp[i-1]+1; for(int j=i-1;j>=1;j--) { if(hw[j][i]) dp[i]=min(dp[i],dp[j-1]+1); } } printf("Case %d: %d\n",cas,dp[len]); } return 0;}
0 0
- lightoj 1044 - Palindrome Partitioning DP优化DP
- lightoj 1044 Palindrome Partitioning(dp)
- LightOJ - 1044 Palindrome Partitioning(DP)
- LightOJ 1044 - Palindrome Partitioning(DP)
- LightOJ 1044 - Palindrome Partitioning【dp】
- LightOJ 1044 - Palindrome Partitioning(dp)
- LightOJ 1044 - Palindrome Partitioning (区间dp)
- lightoj 1044 - Palindrome Partitioning
- lightoj 1044 - Palindrome Partitioning
- 【DP】Palindrome Partitioning I
- 【DP】Palindrome Partitioning II
- DP Palindrome Partitioning II
- Light OJ 1044 Palindrome Partitioning (hash+DP)
- 1044 - Palindrome Partitioning(记忆化DP)
- [leetcode][DP] Palindrome Partitioning II
- leetcode---palindrome-partitioning-ii---dp
- leetcode---palindrome-partitioning---dp, dfs
- leetcode Palindrome Partitioning II DP 深搜
- Python模块:subprocess
- 【java模板引擎】-beetl
- USB设备驱动程序-USB Gadget Driver(二)
- Trident wordCount例子解读
- PostgreSQL pg_bulkload speed test
- lightoj 1044 - Palindrome Partitioning DP优化DP
- Docker初体验1 —几个概念的理解
- JAVA单聊模式的不同写法
- 为什么要三次握手
- IOS各种简单动画的实现
- Linux基础05_磁盘管理
- App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure.
- 泛型问题
- Android开发编程Fragment对象引出新功能