[Learn dynamic programming] Surround the only substring in a string (25)

Table of contents

How to learn dynamic programming?

1. Question analysis

2. Algorithm principle

1. Status display

2. State transition equation

3. Initialization

4. Order of filling in the form

5. Return value

3. Code writing

Write at the end:


How to learn dynamic programming?

There is no shortcut to learning an algorithm, let alone learning dynamic programming.

Come with me to solve dynamic programming algorithm questions and learn dynamic programming together!

1. Question analysis

Question link: 467. Surrounding the only substring in a string - LeetCode 

This question is also easy to understand. You can basically understand it after reading it once. Just look for the examples he gave.

How many different non-empty substrings appear in base? Base is an endless cycle of a ~ za ~ z.

2. Algorithm principle

1. Status display

dp[ i ] represents how many of all substrings ending at position i have appeared in base.

2. State transition equation

This can be divided into two situations:

If length is 1, then dp[i] = 1

If the length is greater than 1, it proves that the i position is combined with the previous position, then if:

s[ i - 1 ] + 1 == s[ i ] || ( s[ i - 1 ] == 'z' && s[ i ] == 'a' ),dp[ i ] = dp[ i - 1 ]

(As many situations as there were before, there are as many situations now)

Because what is sought is the sum of all situations, the state transition equation is:

dp[ i ] = 1 + s[ i - 1 ] + 1 == s[ i ] || ( s[ i - 1 ] == 'z' && s[ i ] == 'a' ) ? dp[ i ] = dp[ i - 1 ] : 0

3. Initialization

Because each letter must appear, we can directly initialize the array to 1,

In this way, our state transition equation does not need to add that extra 1.

4. Order of filling in the form

From left to right.

5. Return value

Because letters may be repeated, we cannot directly return the sum of all elements.

So how should we remove the weight?

For dp values ​​ending with the same character, we can just go to the largest one.                

Create an array of size 26, which stores the maximum dp value at the end of the corresponding character.

Finally, return the sum in the array.

3. Code writing

class Solution {
public:
    int findSubstringInWraproundString(string s) {
        int n = s.size();
        vector<int> dp(n, 1);
        for(int i = 1; i < n; i++) {
            if(s[i - 1] + 1 == s[i] || (s[i - 1] == 'z' && s[i] == 'a'))
                dp[i] += dp[i - 1];
        }

        int hash[26] = { 0 };
        for(int i = 0; i < n; i++) {
            hash[s[i] - 'a'] = max(hash[s[i] - 'a'], dp[i]);
        }

        int sum = 0;
        for(auto e : hash) sum += e;
        
        return sum;
    }
};

Write at the end:

That’s the content of this article, thank you for reading.

If you feel you have gained something, you can give the blogger a like .

If there are omissions or errors in the content of the article, please send a private message to the blogger or point it out in the comment area~

Guess you like

Origin blog.csdn.net/Locky136/article/details/131972044