# 1044 - Palindrome Partitioning (记忆化搜索乱搞)

1044 - Palindrome Partitioning
 Time Limit: 1 second(s) Memory Limit: 32 MB

A palindrome partition is the partitioning of a string suchthat each separate substring is a palindrome.

For example, the string "ABACABA" could bepartitioned 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 possiblenumber 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 uppercaseletters with length no more than 1000.

# Output

For each case of input you have to print the case number andthe desired result.

3

AAAA

ABCDEFGH

QWERTYTREWQWERT

# Output for Sample Input

Case 1: 1

Case 2: 8

Case 3: 5

AC代码:

/* ***********************************************
Author        :xdlove
Created Time  :2015年11月20日 星期五 23时52分33秒
File Name     :lightoj/1044/Palindrome_Partitioning.cpp
************************************************ */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

#define REP_ab(i,a,b) for(int i = a; i <= b; i++)
#define REP(i, n) for(int i = 0; i < n; i++)
#define REP_1(i,n) for(int i = 1; i <= n; i++)
#define DEP(i,n) for(int i = n - 1; i >= 0; i--)
#define DEP_N(i,n) for(int i = n; i >= 1; i--)
#define CPY(A,B) memcpy(A,B,sizeof(B))
#define MEM(A) memset(A,0,sizeof(A))
#define MEM_1(A) memset(A,-1,sizeof(A))
#define MEM_INF(A) memset(A,0x3f,sizeof(A))
#define MEM_INFLL(A) memset(A,0x3f3f,sizeof(A))
#define mid (((l + r) >> 1))
#define lson l, mid, u << 1
#define rson mid + 1, r, u << 1 | 1
#define ls (u << 1)
#define rs (u << 1 | 1)

typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 1e5 + 5;
const int MAXM = MAXN;
const int mod = 1e9 + 7;
int dp[1005][1005];
char s[1105];
bool vis[1005][1005];

int get(int v,int n) {
int l,r;
int len = 0;
l = v - 1,r = v + 1;
while(l >= 0 && r < n && s[l] == s[r]) {
vis[l][r] = true;
l--;
r++;
len += 2;
}
return len;
}

int get1(int v,int n) {
int l,r,len;
len = 0;
l = v,r = v + 1;
while(l >= 0 && r < n && s[l] == s[r]) {
vis[l][r] = true;
l--;
r++;
len += 2;
}
return len;
}

void work(char *s) {
MEM(vis);
MEM_1(dp);
int len = strlen(s);
for(int i = 0; i < len; i++) {
int tp = get(i,len);
vis[i][i] = true;
tp = get1(i,len);
}
}

int dfs(int l,int r) {
int &ans = dp[l][r];
if(l > r) return 0;
if(~ans) return ans;
if(vis[l][r]) return ans = 1;
ans = INF;
for(int i = l; i <= r; i++) {
if(vis[l][i])
ans = min(ans,dfs(i + 1,r) + 1);
}
ans = min(ans,dfs(l + 1,r) + 1);
return ans;
}

void debug() {
for(int i = 0; i < s[i]; i++){
for(int j = 0; j < s[j]; j++){
printf("%d ",vis[i][j]);
}
puts("");
}
}

int main() {
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T,cnt = 0;
cin>>T;
while(T--) {
scanf("%s",s);
work(s);
int n = strlen(s);
printf("Case %d: %d\n",++cnt,dfs(0,n - 1));
//debug();
}
return 0;
}

/* ***********************************************
Author        :xdlove
Created Time  :2015年11月20日 星期五 23时52分33秒
File Name     :lightoj/1044/Palindrome_Partitioning.cpp
************************************************ */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

#define REP_ab(i,a,b) for(int i = a; i <= b; i++)
#define REP(i, n) for(int i = 0; i < n; i++)
#define REP_1(i,n) for(int i = 1; i <= n; i++)
#define DEP(i,n) for(int i = n - 1; i >= 0; i--)
#define DEP_N(i,n) for(int i = n; i >= 1; i--)
#define CPY(A,B) memcpy(A,B,sizeof(B))
#define MEM(A) memset(A,0,sizeof(A))
#define MEM_1(A) memset(A,-1,sizeof(A))
#define MEM_INF(A) memset(A,0x3f,sizeof(A))
#define MEM_INFLL(A) memset(A,0x3f3f,sizeof(A))
#define mid (((l + r) >> 1))
#define lson l, mid, u << 1
#define rson mid + 1, r, u << 1 | 1
#define ls (u << 1)
#define rs (u << 1 | 1)

typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 1e5 + 5;
const int MAXM = MAXN;
const int mod = 1e9 + 7;
int dp[1005][1005];
char s[1105];
bool vis[1005][1005];

int get(int v,int n) {
int l,r;
int len = 0;
l = v - 1,r = v + 1;
while(l >= 0 && r < n && s[l] == s[r]) {
vis[l][r] = true;
l--;
r++;
len += 2;
}
return len;
}

int get1(int v,int n) {
int l,r,len;
len = 0;
l = v,r = v + 1;
while(l >= 0 && r < n && s[l] == s[r]) {
vis[l][r] = true;
l--;
r++;
len += 2;
}
return len;
}

void work(char *s) {
MEM(vis);
MEM_1(dp);
int len = strlen(s);
for(int i = 0; i < len; i++) {
int tp = get(i,len);
vis[i][i] = true;
tp = get1(i,len);
}
}

int dfs(int l,int r) {
int &ans = dp[l][r];
if(l > r) return 0;
if(~ans) return ans;
if(vis[l][r]) return ans = 1;
ans = INF;
for(int i = l; i <= r; i++) {
ans = min(ans,dfs(l,i) + dfs(i + 1,r));
}
return ans;
}

void debug() {
for(int i = 0; i < s[i]; i++){
for(int j = 0; j < s[j]; j++){
printf("%d ",vis[i][j]);
}
puts("");
}
}

int main() {
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T,cnt = 0;
cin>>T;
while(T--) {
scanf("%s",s);
work(s);
int n = strlen(s);
printf("Case %d: %d\n",++cnt,dfs(0,n - 1));
//debug();
}
return 0;
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：1044 - Palindrome Partitioning (记忆化搜索乱搞) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)