1.题目描述

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"]
]
也就是给定一个字符串,找出所有可能的划分,每一个划分的每一个字串都是回文串。设这个划分函数为partition_index(string s,int begin,int end)

2.解题思路

蛮力法当然是可以解决这个问题,对于一个长度为n的字符串,有2n-1种划分方法,只需要判断每种划分是否满足条件即可,虽然不知道暴力法能不能在系统里面ac掉,但是这样显然不是一个正常的程序员该干的事。不过蛮力法会给我们一些启示。我们从为什么蛮力法会有2n-1种划分方法开始分析。

当然,这也很容易分析,对于长度为n的字符串s0s1…sk…sn-1,在sk和sk-1之间我们可以选择截断与不截断,但是,我们可以基于这个思路理出一个递归的思路,那就是,对于字符串sbeginsbegin+1…sbegin+k…sn-1,所有可能满足条件的划分分为两类:

(1)在sbegin和sbegin+1之间有隔断,这种情况,只需sbegin和partition_index(s,begin+1,n-1)连接起来即可

(2)在sbegin和sbegin+1之间无隔断,这种情况,需找到至少包含sbegin和sbegin+1的一个回文字符串,如果能够找到,比如,sbegin…sbegin+k是一个回文字符串,那么,将这个回文字符串和partition_index(s,k+1,n-1)连接起来,注意,可能找到不止一个包含s0和s1的回文字符串,那么有多少个,就多出多少类划分可能;如果找不到,那么返回空vector.

3.代码

#include <iostream>

#include <vector>

#include <string>

#include <iterator>

using namespace std;

vector<vector<string> > partition(string s);

vector<vector<string> > partition_index(string s, int begin, int end);

bool isPalindrome(string s, int begin,int end);

int main()

{

    partition("cdd");

    return 0;

}

 

vector<vector<string> > partition(string s) {

 

    return partition_index(s,0,s.length()-1);

    

}

 

vector<vector<string> > partition_index(string s, int begin, int end)

{

    vector<vector<string> > result;

 

    //边界条件

    if(begin == end)

    {

        vector<string> item;

        item.push_back(s.substr(begin,1));       

        result.push_back(item);

        return result;

    }

    if(begin > end) return result;

    

    //如果在begin与begin+1之间有划分

    vector<vector<string> > result_part1;

    result_part1 = partition_index(s,begin+1,end);

    if(!result_part1.empty())

    {

        vector<vector<string> >::iterator iter_vv;

        for(iter_vv = result_part1.begin();iter_vv!=result_part1.end();++iter_vv)

        { 

            vector<string> temp;

            //temp = *iter_vv;

            temp.push_back(s.substr(begin,1));

            

            vector<string>::iterator iter_v;

            for(iter_v = (*iter_vv).begin();iter_v!= (*iter_vv).end();++iter_v)

            {

                temp.push_back(*iter_v);

            }

            result.push_back(temp);

        }

    }

    

    

    //如果在begin和begin+1之间无划分

    int nextSeg = begin +1;

    while(nextSeg<=end)

    {

 

        //找到了回文字符串

        if(isPalindrome(s,begin,nextSeg))

        {

            vector<vector<string> >result_part2_1;

            result_part2_1  = partition_index(s,nextSeg+1,end);

            if(!result_part2_1.empty())

            {

                vector<vector<string> >::iterator iter_vv;

                for(iter_vv = result_part2_1.begin();iter_vv!=result_part2_1.end();++iter_vv)

                {

                    vector<string> temp;

              //      temp = *iter_vv;

                    temp.push_back(s.substr(begin,nextSeg));

            

                    vector<string>::iterator iter_v;

                    for(iter_v = (*iter_vv).begin();iter_v!= (*iter_vv).end();++iter_v)

                    {

                        temp.push_back(*iter_v);

                    }

                    result.push_back(temp);

                }

                

            }

            else

            {

                vector<string>temp;

                temp.push_back(s.substr(begin,nextSeg-begin+2));

                result.push_back(temp);

            }

        }

        //继续找看是否有其他的更长的回文字符串

        nextSeg++;

    }

    

    return result;

        

}

 

 

//判断字符串由begin 和end 确定的字串是否为回文字符串

bool isPalindrome(string s, int begin,int end)

{

    if(begin>end)return false;

    if(begin==end)return true;

    if((begin+1)==end)return s[begin]==s[end];

    else

        if(s[begin]==s[end])

            return isPalindrome(s,begin+1,end-1);

        esle return false;

}

 

 

另外,代码可以简化,比如,连接结果的代码可以封装成一个函数.

leetcode—Palindrome 解题报告的更多相关文章

  1. LeetCode: Permutations 解题报告

    Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...

  2. 【LeetCode】409. Longest Palindrome 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:字典统计次数 方法二:HashSet 方法三 ...

  3. 【LeetCode】214. Shortest Palindrome 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前缀是否回文 判断前缀 相似题目 参考资料 日期 题 ...

  4. 【LeetCode】125. Valid Palindrome 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 列表生成式 正则表达式 双指针 日期 题目地址:https:/ ...

  5. LeetCode: Valid Palindrome 解题报告

    Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...

  6. LeetCode C++ 解题报告

    自己做得LeetCode的题解,使用C++语言. 说明:大多数自己做得,部分参考别人的思路,仅供参考; GitHub地址:https://github.com/amazingyyc/The-Solut ...

  7. C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告

    剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...

  8. LeetCode: Subsets 解题报告

    Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...

  9. LeetCode: Triangle 解题报告

    Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...

随机推荐

  1. NOIP2014 行记

    不知道OI是啥或者信息学竞赛是啥的可以按`Ctrl+W`. <del>很早开始写的..准备出分之后再发布.</del> 谨以此文纪念我信息学竞赛的第一次正式考试. 背景音乐底部 ...

  2. notepad++ 开启/关闭 记住最后打开的文件

    开启记住最后打开的文件 1) 6.3以前版本如下设置: 设置-->首选项-->其他 把左下角的 "记住最后打开文件" 勾选. 2) 6.3以后版本如下设置: 设置--& ...

  3. 使用rsyslog+loganalzey收集日志显示客户端ip

    http://www.ituring.com.cn/article/128536 rsyslog localhost 转发 http://bigsec.net/one/tool/rsyslog.htm ...

  4. [Ruby on Rails系列]4、专题:Rails应用的国际化[i18n]

    1. 什么是internationalization(i18n)? 国际化,英文简称i18n,按照维基百科的定义:国际化是指在设计软件,将软件与特定语言及地区脱钩的过程.当软件被移植到不同的语言及地区 ...

  5. codeforces #305 A Mike and Frog

    挺简单的题目,但是有一堆恶心的边界 在刨去恶心的边界之后: 假定我们知道两边的循环节为b1,b2 其中h第一次到达目标的时间为a1,a2 又知道对于答案t t=a1+b1*t1=a2+b2*t2 不妨 ...

  6. 前阿里CEO卫哲谈阿里创业经验:如何找人、找钱、找方向?(不同的阶段分别有:时间优先、金额优先、比例优先,不要做平台,太难)

    新浪科技李根 整理报道 卫哲现在是御嘉基金的创始合伙人,他另一个更加知名的身份是阿里巴巴(B2B)前CEO,在2006年到2011年的时间里,卫哲见证了阿里巴巴如何利用人才.资本和方向选择一路壮大. ...

  7. HDU4857——逃生(反向建图+拓扑排序)(BestCoder Round #1)

    逃生 Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前.同时,社会 ...

  8. 使用PHP处理POST上传时$_FILES数组为何为空

    在做一个简单的表单上传测试时,服务端的php脚本中,$_FILES数组为空;这样就不能获取从浏览器上传的信息.什么原因呢? 通过Google,找到下面这个web: php上传文件$_FILES数组为空 ...

  9. Mac修改用户名

    Mac 修改用户是一件很悲剧的事,因为牵涉到很多地方的修改,当然,如果只是需要满足登陆用户名的修改的话,就比较简单.而如果需要将某个用户在每一个地方显示的名字都改掉的话,就要修改不是地方了,下面就来讲 ...

  10. bzoj3205

    和bzoj2595类似,也是斯坦纳树 设f[l,r,]表示在点i机器人组合成了l-r最少推的次数,然后可得 f[l,r,i]=min(f[l,m,i]+f[m+1,r,i]) f[l,r,i]=min ...