首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

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

2013-03-19 
判断字符串string所有分割和子串是否为回文(palindrome)并返回结果(Palindrome Partitioning)在网站http:/

判断字符串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)


热点排行