当前位置:网站首页>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 )
边栏推荐
- MySQL 错误总结
- Intel将逐步结束Optane存储业务 未来不再开发新产品
- Want to know how to open an account through online stock? Excuse me, is it safe to open a stock account by mobile phone?
- 01背包关于从二维优化到一维
- Chrony time synchronization
- Can the access database be accessed remotely
- 不同数据库相同字段查不重复数据
- 多标签用户画像分析跑得快的关键在哪里?
- Restful style details
- Ar virtual augmentation and reality
猜你喜欢

数学建模——聚类

Day15 (day16 extension): file contains vulnerability

LeetCode刷题(6)

2022危险化学品经营单位主要负责人操作证考试题库及答案

Google browser cross domain configuration free

Normal visualization

6.2 function-parameters

Flask reports an error runtimeerror: the session is unavailable because no secret key was set

ADB common command list

Leetcode question brushing (6)
随机推荐
Centos7/8 command line installation Oracle11g
The biggest upgrade of Bluetooth over the years: Bluetooth Le audio is about to appear in all kinds of digital products
Quaternion and its simple application in unity
Cloud security daily 220712: the IBM integration bus integration solution has found a vulnerability in the execution of arbitrary code, which needs to be upgraded as soon as possible
Mathematical modeling clustering
Requests library simple method usage notes
Squareline partners with visual GUI development of oneos graphical components
Error reporting when adding fields to sap se11 transparent table: structural changes at the field level (conversion table xxxxx)
Clickhouse learning (II) Clickhouse stand-alone installation
Sudoku (DFS)
CVPR 2022 | clonedperson: building a large-scale virtual pedestrian data set of real wear and wear from a single photo
01背包关于从二维优化到一维
Basic shell operations (Part 2)
2022电工(初级)考题模拟考试平台操作
Application of matrix transpose
Simple unit testing idea
Clickhouse learning (I) Clickhouse?
2022 question bank and answers of operation certificate examination for main principals of hazardous chemical business units
[opencv] - Operator (Sobel, canny, Laplacian) learning
SAP ooalv-sd module actual development case (add, delete, modify and check)