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

原创 2015年11月21日 02:45:14
1044 - Palindrome Partitioning
PDF (English) Statistics Forum
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.


Sample Input

3

AAAA

ABCDEFGH

QWERTYTREWQWERT

Output for Sample Input

Case 1: 1

Case 2: 8

Case 3: 5

题意:  给定一个只包含大写字母的字符串,求它的最小划分回文子串;

题解:  dp[i][j]表示子串i~j的最小划分回文子串,则答案为dp[0][n-1]; 预先n^2处理出所有回文区间,然后进行状态的转移;


AC代码:

/* ***********************************************
Author        :xdlove
Created Time  :2015年11月20日 星期五 23时52分33秒
File Name     :lightoj/1044/Palindrome_Partitioning.cpp
************************************************ */
 
#pragma comment(linker, "/STACK:1024000000,1024000000")
#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;
}

超时代码(状态转移方程不同而已,时间差了差不多2s,ORZ):

/* ***********************************************
Author        :xdlove
Created Time  :2015年11月20日 星期五 23时52分33秒
File Name     :lightoj/1044/Palindrome_Partitioning.cpp
************************************************ */
 
#pragma comment(linker, "/STACK:1024000000,1024000000")
#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;
}



版权声明:追逐心中的梦想,永不放弃! By-xdlove 举报

相关文章推荐

lightoj1044--Palindrome Partitioning(记忆化搜索)

Description A palindrome partition is the partitioning of a string such that each separate substrin...

hdu1044(记忆化搜索+广搜)

Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

HDU 4632 Palindrome subsequence 记忆化搜索

Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/...

LightOJ 1044 - Palindrome Partitioning (区间dp)

题意: n<=1000长度的字符串,最少能划分成几个回文子串n<=1000长度的字符串, 最少能划分成几个回文子串 分析: 区间dp预处理出isPalindrome[l][r]:=s[l⋯r]...
  • lwt36
  • lwt36
  • 2015-11-13 20:13
  • 282

LightOJ 1044 - Palindrome Partitioning(DP)

题意:求出最少可以将s划分成几个回文串。 思路:dp[i]表示从s[1...i]最少可以划分成几个回文串。则对1 #include #include #include #include #i...

LA3942 Remember the Word(字典树+记忆化搜索)

题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=22109 题意:给出一个由S个不同单词组成的字典和一个长字符串。把这个...

HDU 4597(记忆化搜索 dfs 参考)

HDU 4597(记忆化搜索 dfs 参考)

FatMouse and Cheese_hdu_1078(记忆化搜索).java

FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)