leetcode132. Palindrome Partitioning II
leetcode132. Palindrome Partitioning II
题意:
给定一个字符串s,分区使分区的每个子字符串都是回文。
返回对于s的回文分割所需的最小削减。
例如,给定s =“aab”,
返回1,因为可以使用1切割生成回文分割[“aa”,“b”]。
思路:
一开始我用dfs + 贪心来做,找到最长的回文,然后拆掉继续dfs,然后最后一个测试样例过不了,不是复杂度的问题,是算法本身有问题。
看了别人的思路,是用dp来做的,设DP[i][j]表示第i个字符到第j个字符是否构成回文,若是,则DP[i][j]=1;若否,则DP[i][j]=0;如此,根据回文的约束条件(对称性),DP[i][j]构成回文需满足:
1、输入字符串s[i]==s[j],对称性;
2、条件1满足并不能保证i到j构成回文,还须:(j-i)<=1或者DP[i+1][j-1]=1;即,i、j相邻或者i=j,也就是相邻字符相等构成回文或者字符自身构成回文,如果i、j不相邻或者相等,i到j构成回文的前提就是DP[i+1][j-1]=true
所以状态转移方程:DP[i][j]=(s[i]s[j]&&(j-i<=1||DP[i+1][j-1]1))。由于i必须小于j,i>=0&&i<s.length().如此,DP[i][j]构成一个下三角矩阵,空间、时间复杂度均为O(n2),如下所示:对于长度为4的字符串s=“baab”,其中红色部分为i>j,为无意义部分;绿色部分i==j,即字符串自身必然构成回文,DP[i][j]=1;白色部分,为长度大于1的子串,需要状态转移方程进行判断。
ac代码:
C++
class Solution {
public:
int minCut(string s) {
int len = s.length();
vector<vector<bool> >map(len,vector<bool>(len));
for(int i = len - 1; i >= 0; i--)
{
for(int j = i; j < len; j++)
{
if(i == j)
{
map[i][j] = true;
}
else if(i + 1 == j)
{
if(s[i] == s[j])
{
map[i][j] = true;
}
}
else
{
if(s[i] == s[j] && map[i + 1][j - 1])
{
map[i][j] = true;
}
}
}
}
vector<int> res(len + 1,0);
for(int i = len - 1; i >= 0; i--)
{
res[i] = res[i + 1] + 1;
for(int j = i + 1; j < len; j++)
{
if(map[i][j] == true && res[j + 1] + 1 < res[i])
res[i] = res[j + 1] + 1;
}
}
return res[0] - 1;
}
};
python
参考博文
leetcode132. Palindrome Partitioning II的更多相关文章
- 19. Palindrome Partitioning && Palindrome Partitioning II (回文分割)
Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...
- LeetCode:Palindrome Partitioning,Palindrome Partitioning II
LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...
- 【leetcode】Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II
https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every ...
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 动态规划——Palindrome Partitioning II
Palindrome Partitioning II 这个题意思挺好理解,提供一个字符串s,将s分割成多个子串,这些字串都是回文,要求输出分割的最小次数. Example:Input: "a ...
- leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II
131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...
- LeetCode: Palindrome Partitioning II 解题报告
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- 【LeetCode】132. Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
随机推荐
- usb_submit_urb 解释的够够的
/** * usb_submit_urb - issue an asynchronous transfer request for an endpoint * @urb: pointer to the ...
- linux动态库编译和使用详细剖析 - 后续
引言 - 也许是修行 很久以前写过关于动态库科普文章, 废话反正是说了好多. 核心就是在 linux 上面玩了一下 dlopen : ) linux动态库编译和使用详细剖析 - https://www ...
- java获取weblogic应用运行路径
String url = TemplateBuilder(当前类).class.getClassLoader().getResource("").getPath(); String ...
- POJ 2186 Popular cows(Kosaraju+强联通分量模板)
题目链接:http://poj.org/problem?id=2186 题目大意:给定N头牛和M个有序对(A,B),(A,B)表示A牛认为B牛是红人,该关系具有传递性,如果牛A认为牛B是红人,牛B认为 ...
- 理解 pkg-config 工具(linux编译辅助工具)
转:http://www.jb51.net/LINUXjishu/86519.html 你在 Unix 或 Linux 下开发过软件吗?写完一个程序,编译运行完全正常,在你本机上工作得好好的,你放到源 ...
- 百度NLP面试题
C++ : 1.拷贝构造函数和重载=符分别在什么情况下被调用,实现有什么区别 2.虚函数的目的,虚函数和模板类的区别,如何找到虚函数 常规算法: 1. 如何输出一个集合的所有真子集,递归和非递 ...
- hdu5984
听说大佬都是看到1.693147就知道是ln(2)+1我是服气的 不过老老实实推的话就看你大一数分/高数是不是学扎实了 令 把L移到右边并两边求导可得,即 令 代入最开始的公式得到 化简可得,得解 # ...
- python 分词库jieba
算法实现: 基于Trie树结构实现高效的词图扫描,生成句子中汉字所有可能成词情况所构成的有向无环图(DAG) 采用了动态规划查找最大概率路径, 找出基于词频的最大切分组合 对于未登录词,采用了基于汉字 ...
- 常用开放api【长期更新】
获取时间: 苏宁:http://quan.suning.com/getSysTime.do 淘宝:http://api.m.taobao.com/rest/api3.do?api=mtop.commo ...
- EditText属性描述
android:layout_gravity="center_vertical"//设置控件显示的位置:默认top,这里居中显示,还有bottom android:hint=&qu ...