首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【leetcode】Palindrome Partitioning II

【leetcode】Palindrome Partitioning II

作者头像
阳光岛主
发布2019-02-19 11:37:27
3730
发布2019-02-19 11:37:27
举报
文章被收录于专栏:米扑专栏米扑专栏米扑专栏

Question : 

Given a string s, partition s such that every substring of the partition is a palindrome.

Return the minimum cuts needed for a palindrome partitioning of s.

For example, given s = "aab", Return 1 since the palindrome partitioning ["aa","b"] could be produced using 1 cut.

Anwser 1 :

class Solution {
public:
    int minCut(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (s.size() < 2)
            return 0;
        int length = s.size();
        int *minSegs = new int[length+1];
        minSegs[0] = 0;
        minSegs[1] = 1;
        int **dp = new int*[2];
        dp[0] = new int[length+2];
        dp[1] = new int[length+2];
        dp[0][0] = dp[1][0] = 0;
        dp[0][1] = dp[1][1] = 1;
        dp[0][2] = -1;
        
        int cur = 0;
        int pre = 1;
        for (int i=1; i<s.size(); i++) {
            int cur = i % 2;
            int pre = (i +1) % 2;
            minSegs[i+1] = minSegs[i] + 1;
            int n = 1;
            for (int j=0; dp[pre][j] != -1; j++) {
                int left = i-1-dp[pre][j];
                if (left>=0 && s[left]==s[i]) {
                    dp[cur][++n] = dp[pre][j] + 2;
                    if (minSegs[left] + 1 < minSegs[i+1])
                        minSegs[i+1] = minSegs[left] + 1;
                }
            }
            dp[cur][++n] = -1;
        }
        
        int min = minSegs[s.size()] - 1;
        delete []minSegs;
        return min;
    }
};

Anwser 2: :

class Solution {
public:
    int minCut(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
		int len=s.length();
		int BIG=2*len;
		int num_line=len+1;
		vector<int> isPalindrome(num_line*num_line);
		for(int l=1;l<=len;l++)//两个木板之间有多少个字符
		{
			for (int i=0;i+l<num_line;i++)//从第0个木板开始,i+l为木板的序号
			{
				if (l==1)
				{
					isPalindrome[i*num_line+(i+l)]=1;
				}
				else if(l==2)
				{
					if (s[i]==s[i+l-1])
						isPalindrome[i*num_line+(i+l)]=1;
					else
						isPalindrome[i*num_line+(i+l)]=BIG;
				}
				else
				{
					if (s[i]==s[i+l-1])
						isPalindrome[i*num_line+(i+l)]=isPalindrome[(i+1)*num_line+(i+l-1)];
					else
						isPalindrome[i*num_line+(i+l)]=BIG;
				}
			}
		}
		vector<int> dist(num_line);
		vector<bool> labeled(num_line);
		for(int i=0;i<num_line;i++)
		{
			dist[i]=BIG;
			labeled[i]=false;
		}
		dist[0]=0;
		labeled[0]=true;
		int current_line=0;
		for(int i=1;i<num_line;i++)
		{	
			for(int d=current_line+1;d<num_line;d++)//对所有的相邻(在图中)的木板
			{
				if(!labeled[d])
				{
					if (dist[current_line]+isPalindrome[current_line*num_line+d]<dist[d])
					{
						dist[d]=dist[current_line]+isPalindrome[current_line*num_line+d];
					}
				}
			}
			int mindist=BIG;
			int minindex=-1;
			for(int i=0;i<num_line;i++)
			{
				if(!labeled[i])
				{
					if(mindist>dist[i])
					{
						minindex=i;
						mindist=dist[i];
					}
				}
			}
			current_line=minindex;
			labeled[minindex]=true;
		}

		return dist[num_line-1]-1;
	}
};

参考推荐:

leetcode:Palindrome Partitioning II

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013年04月14日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档