Light oj 1044 - Palindrome Partitioning(记忆化)
来源:互联网 发布:网络与共享中心打不开 编辑:程序博客网 时间:2024/05/24 02:15
A palindrome partition is the partitioning of a string such that each separate substring is a palindrome.
For example, the string "ABACABA" could be partitioned in several different ways, such as {"A","B","A","C","A","B","A"}, {"A","BACAB","A"}, {"ABA","C","ABA"}, or {"ABACABA"},among others.
You are given a string s. Return the minimum possible number of substrings in a palindrome partition of s.
Input
Input starts with an integer T (≤ 40), denoting the number of test cases.
Each case begins with a non-empty string s of uppercase letters with length no more than 1000.
Output
For each case of input you have to print the case number and the desired result.
Sample Input
Output for Sample Input
3
AAAA
ABCDEFGH
QWERTYTREWQWERT
Case 1: 1
Case 2: 8
Case 3: 5
#pragma comment(linker, "/STACK:1024000000,1024000000")#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<stack>#include<vector>#include<set>#include<map>#define L(x) (x<<1)#define R(x) (x<<1|1)#define MID(x,y) ((x+y)>>1)#define bug printf("hihi\n")#define eps 1e-8typedef long long ll;using namespace std;#define INF 0x3f3f3f3f#define N 1005int dp[N][N],ok[N][N];int len;char c[N];void inint(){ int i,j; memset(ok,0,sizeof(ok)); for(int i=0;i<len;i++) ok[i][i]=1; for(int i=0;i<len-1;i++) ok[i][i+1]=c[i]==c[i+1] ? 1:0; for(int k=3;k<=len;k++) for(int i=0;i+k<=len;i++) { int j=i+k-1; ok[i][j]= ok[i+1][j-1]&&c[i]==c[j] ? 1:0; }}int dfs(int s,int e){ if(s>e) return 0; if(s==e) return dp[s][e]=1; if(dp[s][e]>=0) return dp[s][e]; dp[s][e]=INF; for(int j=s;j<=e;j++) if(ok[s][j]) dp[s][e]=min(dp[s][e],1+dfs(j+1,e)); return dp[s][e];}int main(){ int i,j,t,ca=0; scanf("%d",&t); while(t--) { scanf("%s",c); len=strlen(c); inint(); memset(dp,-1,sizeof(dp)); printf("Case %d: %d\n",++ca,dfs(0,len-1)); } return 0;}
- Light oj 1044 - Palindrome Partitioning(记忆化)
- Light OJ 1044 Palindrome Partitioning (hash+DP)
- Light Oj 1044 - Palindrome Partitioning(回文串)
- 1044 - Palindrome Partitioning (记忆化搜索乱搞)
- 1044 - Palindrome Partitioning(记忆化DP)
- Light oj 1044 - Palindrome Partitioning (多校连萌,求最少回文子串的个数)
- lightoj1044--Palindrome Partitioning(记忆化搜索)
- LeetCode OJ:Palindrome Partitioning
- LeetCode OJ Palindrome Partitioning
- light oj 1043 Triangle Partitioning
- LIGHT OJ 1199 - Partitioning Game
- LeetCode OJ:Palindrome Partitioning II
- LeetCode OJ - Palindrome Partitioning II
- LeetCode OJ Palindrome Partitioning II
- light oj 1043 - Triangle Partitioning【数学】
- 【Light-oj】-1043 - Triangle Partitioning(数学)
- light oj 1011 - Marriage Ceremonies (状态压缩+记忆化搜索)
- Light oj 1031 Easy Game(记忆化)
- 代码生成XML
- mysql 给已存在的商品数据,根据商品ID增加商品编号
- Python编辑器--Ulipad
- LoadRunner常见问题
- 随笔-2015-9-7
- Light oj 1044 - Palindrome Partitioning(记忆化)
- 正确使用SQLCipher来加密Android数据库
- ASM模型对人脸特征点的检测
- 模板输出U方法带锚点 thinkphp
- IOS_UI_UIScrollview
- c++模板和java泛型之间有何不同
- ubuntu忘记开机密码
- java静态类与非静态类
- StoryBoard学习1 tabbar实现