判断字符串string所有分割和子串是否为回文(palindrome)并返回结果(Palindrome Partitioning)

在网站http://leetcode.com/onlinejudge上

原题1是这样的:(解答见函数partition)

Palindrome Partitioning

Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
For example, given s = "aab",
Return
  [
    ["aa","b"],
    ["a","a","b"]
  ]

翻译一下:给定字符串string s,把s进行任意分割,如果每个分割后的字串是回文,则返回这样的分割。


原题2是这样的:(解答见函数minCut)

Palindrome Partitioning II

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.

翻译一下:题目如上,只是返回的是最小的划分为多少


注:所谓回文,即无论正序还是逆序字符串的结果是一样的。

这是一个典型的递归问题,解答代码如下:

注:本工程共三个文件:main.cpp demo.h demo.cpp  (开发工具Dev-C++5.4.0)

//main.cpp 
#include <iostream>
#include <string>
#include "demo.h"
#include <vector>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

using namespace std;
int main(int argc, char *argv[]) {
	demo foo1;
	
	string s("aab");
	
	if(foo1.is_palindrome(s))
	{
		cout<<"string:"<<s<<" is palindrome"<<endl;
	}else{
		cout<<"string:"<<s<<" is not palindrome"<<endl;
	}
	
	cout<<"input"<<endl;
	cout<<"\""<<s<<"\""<<endl;
	 
	vector< vector<string> > res;
	res = foo1.substring(s);
	foo1.show_vector(res);
	
	vector< vector<string> > res2;
	res2 = foo1.part(s);
	foo1.show_vector(res2); 
	
	vector< vector<string> > res3;
	res3 = foo1.partition(s);
	foo1.show_vector(res3);
	
	int res4;
	res4 = foo1.minCut(s);
	cout<<"minimum cuts="<<res4<<endl; 
	return 1;
}

//demo.h 
#ifndef DEMO_H_
#define DEMO_H_

#include<vector>
#include<string>

using namespace std;

class demo
{
private:
    vector< vector<string> > merge(string, vector< vector<string> >);

public:
    demo();
    demo(int);
    ~demo();
    bool is_palindrome(string s);
    void show_vector(vector< vector<string> > s);
    vector< vector<string> > part(string s);
    vector< vector<string> > substring(string s);
    vector< vector<string> > partition(string s);
    int minCut(string s);
};
#endif

//demo.cpp
#include <iostream>
#include "demo.h"
#include <fstream>
#include <string>
#include <vector>
#include <climits>

using namespace std;

//constructors
demo::demo()
{
}

vector< vector<string> > demo::substring(string s)
{
    //return the substrings which is palindrome
    vector< vector<string> > result;
    for(int i=0; i < s.size(); i++)
        {
            vector<string> temp;
            int sub_length = i+1;
            bool b_first = true;

            for (int j=0; j<=s.size()-sub_length; j++)
                {
                    string sub=s.substr(j,sub_length);
                    if (this->is_palindrome(sub))
                        {
                            temp.push_back(sub);
                        }
                }
            if(temp.size() > 0)
                {
                    result.push_back(temp);
                }
        }
    return result;
}

vector< vector<string> > demo::partition(string s)
{
    //Palindrome Partitioning
    //return the partitions which each kind of substring is palindrome
    vector< vector<string> > result;
    vector< vector<string> > sub_str;
    sub_str = this->part(s);
    for (int i=0; i<sub_str.size(); i++)
        {
            bool b_add = true;
            for (int j=0; j<sub_str[i].size(); j++)
                {
                    if(!this->is_palindrome(sub_str[i][j]))
                        b_add = false;
                }
            if(b_add)
                result.push_back(sub_str[i]);
        }
    return result;
}

int demo::minCut(string s)
{
    //Palindrome Partitioning II
    // return the minimum cuts needed for a palindrome partitioning of s
    int result;
    vector< vector<string> > partition_res;
    partition_res = this->partition(s);

    int min_len_value =  INT_MAX;
    for (int i=0; i<partition_res.size(); i++)
        {
            if (partition_res[i].size() < min_len_value)
                {
                    min_len_value = partition_res[i].size();
                }
        }

    result = min_len_value;
    return result;
}

vector< vector<string> > demo::part(string s)
{
    // find all sub_partition for string s
    vector< vector<string> > result;
    for(int i=0; i < s.size(); i++)
        {
            if (s.size()-1 == i)  // when i at the end position
                {
                    vector<string> tmp;
                    tmp.push_back(s);
                    result.push_back(tmp);
                    return result;
                }
            vector< vector<string> > temp2;
            int sub_length = i+1;
            string sub_str;
            string left_str;
            sub_str = s.substr(0,sub_length);
            left_str = s.substr(sub_length,s.size()-sub_length);
            temp2 = merge(sub_str,this->part(left_str));

            for (int j=0; j<temp2.size(); j++)
                {
                    result.push_back(temp2[j]);
                }
        }
    return result;
}

vector< vector<string> > demo::merge(string s, vector< vector<string> > v)
{
    vector< vector<string> > result;
    for (int i=0; i < v.size(); i++)
        {
            vector<string> temp;
            temp.push_back(s);
            for (int j=0; j<v[i].size(); j++)
                {
                    temp.push_back(v[i][j]);
                }
            result.push_back(temp);
        }
    return result;
}

bool demo::is_palindrome(string s)
{
    //judge is it a palindrome
    for(int i=0; i<s.size()/2; i++)
        {
            if (s[i] != s[s.size()-(i+1)])
                return false;
        }
    return true;
}

void demo::show_vector(vector< vector<string> > s)
{
    // show the s content.
    cout<<"output"<<"  len="<<s.size()<<endl;
    cout<<"[";
    for (int i=0; i<s.size(); i++)
        {
            cout<<"[";
            for (int j=0; j<s[i].size(); j++)
                {
                    cout<<"\""<<s[i][j]<<"\"";
                    if ( j != (s[i].size()-1) )
                        cout<<",";
                }
            cout<<"]";
            if (i != (s.size()-1))
                cout<<",";

        }
    cout<<"]"<<endl;
}

//deconstructor
demo::~demo()
{
    cout<<"Bye!"<<endl;
}

运行结果如下:


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值