Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of splitting back string by C++

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "C++ to achieve the method of splitting the text string", so the editor summarizes the following contents for you. The content is detailed, the steps are clear, and it has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "C++ to achieve split back string method" article.

The second part of the Palindrome Partitioning II split text string

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.

Example:

Input: "aab"

Output: 1

Explanation: The palindrome partitioning ["aa", "b"] could be produced using 1 cut.

This problem is to find the minimum number of cuts to split the original string into palindromes. If we first consider using brute force to do it, it will be very complicated, because we not only have to judge whether the substring is a palindrome string, but also to find out the minimum number of cuts. So for this kind of playing string and is to find the extreme value of the problem, it is necessary to sacrifice the ancient artifact dynamic programming Dynamic Programming, seconds days seconds air, DP in the hands of the world I have. OK, after a wave of blowing, let's start to do the exercises. The two steps of DP solution, defining the dp array and finding the state transition equation. First of all, let's define the dp array. Here we use the most direct definition method, the one-dimensional dp array, where dp [I] represents the minimum number of partitions within the range of the substring [0, I], then what we finally have to return is dp [n-1]. Here we first add a corner case judgment. If the s string is empty, there is no empty string detection in the test case of 0Jing OJ directly, but the blogger thinks it is better to add it. After all, an empty string is a kind of palindrome string, so it makes sense that the minimum number of partitions is 0. Then there is the big difficulty, how to find out the state transition equation.

How to update dp [I], I said earlier that it represents the minimum number of partitions within the range of the substring [0, I]. So every position in this interval can be divided, so we use a variable j to traverse from 0 to I, so we can divide the interval [0, I] into two parts, [0, jmur1] and [j, I], so suppose we already know the minimum partition number DP [j-1] of interval [0, jmur1], because we update it from the back, and j is less than or equal to I. So DP [j-1] must have been calculated before dp [I]. In this way, we only need to determine whether the substring in the interval [j, I] is a palindrome string, and if so, dp [I] can be updated with 1 + DP [j-1]. The method of judging the substring is the same as that of the previous Palindromic Substrings, using a two-dimensional dp array p, where p [I] [j] indicates whether the substring in the interval [I, j] is a palindrome string, and its state transition equation is p [I] [j] = (s [I] = s [j]) & p [I] [j] [jmur1], where p [I] [j] = true if [I, j] is a palindrome. In that case, this problem is actually equivalent to using two DP methods at the same time, which is really a lot of difficulty.

The first for loop traverses I, and now we initialize dp [I] to I, because for the interval [0, I], even if we cut each letter (why does it sound like Ling Chi?! Can be split up to I times, and no more than this number is needed But it may become smaller, so the second for loop uses j to traverse the interval [0, j]. According to the above explanation, what we need to verify is whether the substring in the interval [j, I] is a palindrome string, then as long as s [j] = s [I], and iMuj

< 2 或者 p[j+1][i-1] 为true的话,先更新 p[j][i] 为true,然后在更新 dp[i],这里需要注意一下corner case,当 j=0 时,我们直接给 dp[i] 赋值为0,因为此时能运行到这,说明 [j, i] 区间是回文串,而 j=0, 则说明 [0, i] 区间内是回文串,这样根本不用分割啊。若 j 大于0,则用 dp[j-1] + 1 来更新 dp[i],最终返回 dp[n-1] 即可,参见代码如下: 解法一: class Solution {public: int minCut(string s) { if (s.empty()) return 0; int n = s.size(); vector p(n, vector(n)); vector dp(n); for (int i = 0; i < n; ++i) { dp[i] = i; for (int j = 0; j = 0; --i) { dp[i] = n - i - 1; for (int j = i; j < n; ++j) { if (s[i] == s[j] && (j - i = 0 && i + len < n && s[i - len] == s[i + len]; ++len) { dp[i + len + 1] = min(dp[i + len + 1], 1 + dp[i - len]); } for (int len = 0; i - len >

= 0 & & I + len + 1 < n & & s [I-len] = = s [I + len + 1]; + + len) {dp [I + len + 2] = min (DP [I + len + 2], 1 + dp [I-len]);}} return dp [n];}} The above is about the content of this article on "C++ method to split the text string". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report