当前位置:网站首页>Leetcode:132. split palindrome string II
Leetcode:132. split palindrome string II
2022-07-29 08:51:00 【Oceanstar's study notes】
Title source
Title Description
class Solution {
public:
int minCut(string s) {
}
};
title
Analyze the amount of data
- 1 0 3 10^3 103, So the algorithm has the highest time complexity O ( N 2 ) O(N^2) O(N2), It can be used dp To do it
Analyze the meaning of the question
Try the model from left to right
Ideas
class Solution {
bool ispalindrome(std::string str, int i, int j){
if(i == j){
return true;
}
while ( i < j){
if(str[i] != str[j]){
return false;
}
i++;
j--;
}
return true;
}
// from str[begin....] At least a few palindromes can be formed at the beginning
int process(std::string &str, int begin){
int N = str.size();
if(begin == N){
return 0;
}
if(begin == N - 1){
return 1;
}
int next = INT32_MAX;
for (int end = begin; end < str.size(); ++end) {
if(ispalindrome(str, begin, end)){
next = std::min(next, 1 + process(str, end + 1));
}
}
return next;
}
public:
int minCut(string s) {
if(s.size() < 2){
return 0;
}
return process(s, 0) - 1;
}
};
Will timeout ( Time complexity O(N^3)).
class Solution {
bool ispalindrome(std::string str, int i, int j){
if(i == j){
return true;
}
while ( i < j){
if(str[i] != str[j]){
return false;
}
i++;
j--;
}
return true;
}
public:
int minCut(string str) {
if(str.size() < 2){
return 0;
}
int N = str.size();
std::vector<int> dp(N + 1);
dp[N] = 0;
dp[N - 1] = 1;
for (int begin = N - 2; begin >= 0; --begin) {
int next = INT32_MAX;
for (int end = begin; end < N; ++end) {
if(ispalindrome(str, begin, end)){
next = std::min(next, 1 + dp[end + 1]);
}
}
dp[begin] = next;
}
return dp[0] - 1;
}
};
Or overtime
Pretreatment structure : Definition dp[i][j] From [i…j] Is it a palindrome
(0)dp The length is N - 1, The height is N - 1
(1) When i < j i < j i<j when , Cannot form effective interval , So useless
(2) initialization dp surface
- First step : Fill diagonal ,dp[i][i] = true
- The second step : Fill in the penultimate diagonal (j = i + 1): When str[i] == str[i + 1] when ,dp[i][j + 1] = true
(3) about dp[i][j] How to fill in the general situation
- If str[i] != dp[j], fill false
- otherwise ,dp[i][j] = dp[i + 1][j - 1]
class Solution {
std::vector<std::vector<bool>> checkPalindrome(string str){
int N = str.size();
std::vector<std::vector<bool>> dp(N, std::vector<bool>(N));
for (int i = 0; i <= N - 1; ++i) {
dp[i][i] = true;
}
for (int i = 0; i <= N - 2; ++i) {
dp[i][i + 1] = str[i] == str[i + 1];
}
for (int i = N - 2; i >= 0; --i) {
for (int j = i + 2; j <= N - 1; ++j) {
dp[i][j] = str[i] == str[j] && dp[i + 1][j - 1];
}
}
return dp;
}
public:
int minCut(string str) {
if(str.size() < 2){
return 0;
}
int N = str.size();
std::vector<int> dp(N + 1);
dp[N] = 0;
dp[N - 1] = 1;
auto check = checkPalindrome(str);
for (int begin = N - 2; begin >= 0; --begin) {
int next = INT32_MAX;
for (int end = begin; end < N; ++end) {
if(check[begin][end]){
next = std::min(next, 1 + dp[end + 1]);
}
}
dp[begin] = next;
}
return dp[0] - 1;
}
};
Expand
Returns a possible segmentation
Examples to be added
Realization
class Solution {
std::vector<std::vector<bool>> checkPalindrome(string str){
int N = str.size();
std::vector<std::vector<bool>> dp(N, std::vector<bool>(N));
for (int i = 0; i <= N - 1; ++i) {
dp[i][i] = true;
}
for (int i = 0; i <= N - 2; ++i) {
dp[i][i + 1] = str[i] == str[i + 1];
}
for (int i = N - 2; i >= 0; --i) {
for (int j = i + 2; j <= N - 1; ++j) {
dp[i][j] = str[i] == str[j] && dp[i + 1][j - 1];
}
}
return dp;
}
public:
std::vector<std::string> minCut(string str) {
int N = str.size();
std::vector<int> dp(N + 1);
dp[N] = 0;
dp[N - 1] = 1;
auto check = checkPalindrome(str);
for (int begin = N - 2; begin >= 0; --begin) {
int next = INT32_MAX;
for (int end = begin; end < N; ++end) {
if(check[begin][end]){
next = std::min(next, 1 + dp[end + 1]);
}
}
dp[begin] = next;
}
std::vector<std::string> ans;
for (int i = 0, j = 1; j <= N; ++j) {
if(check[i][j - 1] && dp[i] == dp[j] + 1){
ans.emplace_back(str.substr(i, j - i));
i = j;
}
}
return ans;
}
};
Returns all possible results ( To be completed )
边栏推荐
- 分组背包
- Design of distributed (cluster) file system
- Source code compilation pytorch pit
- 英语高频后缀
- C language macro define command exercise
- [from_bilibili_dr_can][[advanced control theory] 9_ State observer design] [learning record]
- C language calculates the length of string
- Flask reports an error runtimeerror: the session is unavailable because no secret key was set
- Leetcode question brushing (6)
- 用户身份标识与账号体系实践
猜你喜欢
Excellent Allegro skill recommendation
Lesson 3 threejs panoramic preview room case
Day13: file upload vulnerability
Sword finger offer 26. substructure of tree
Intel将逐步结束Optane存储业务 未来不再开发新产品
SAP sm30 brings out description or custom logical relationship
[opencv] - Operator (Sobel, canny, Laplacian) learning
LeetCode刷题(6)
2022危险化学品经营单位主要负责人操作证考试题库及答案
BI data analysis practitioners learn financial knowledge from scratch? What introductory books are recommended
随机推荐
Crawling JS encrypted data of playwright actual combat case
[from_bilibili_dr_can][[advanced control theory] 9_ State observer design] [learning record]
Day6: use PHP to write file upload page
Qpalette learning notes
Vs2019 compilation cryengine failure problem handling
Error reporting when adding fields to sap se11 transparent table: structural changes at the field level (conversion table xxxxx)
C language macro define command exercise
QT version of Snake game project
Rocky基础之编译安装apache
Txt plain text operation
Simple operation of SQL server data table
6.2 function-parameters
What are the backup and recovery methods of gbase 8s database
7.2-function-overloading
Mathematical modeling - Differential Equations
2022 Shandong Province safety officer C certificate work certificate question bank and answers
Quaternion and its simple application in unity
分组背包
Database system design: partition
2022电工(初级)考题模拟考试平台操作