简体   繁体   中英

How to understand the recursiveness of this function?

This is a program to find the least number of characters that should be inserted (at any position) to convert the given string into a palindrome. Can somebody explain to me what goes on more in detail inside the palindrome function? I have a hard time understanding the recursive function being called again inside the else condition. Thank you!

#include <stdio.h>
#include <string.h>


int min(int a, int b) 
{ 
    if(a > b)
        return b;
    else
        return a;
} 

int palindrome(char string[], int l, int h) 
{
    if (l >= h)
        return 0; 
    if(string[l] == string[h])
        return palindrome(string, l+1, h-1);
    else
    {
        int choice1 = 1 + palindrome(string, l+1, h);
        int choice2 = 1 + palindrome(string, l, h-1);
        return min(choice1, choice2);
    }  
} 

int main() 
{ 
    char string[20];
    printf("ENTER STRING: ");
    scanf("%s", string);
    printf("%d", palindrome(string, 0, strlen(string) - 1)); 
    return 0; 
} 

This piece of code solves the following problem:

You are given a string. How few characters must you insert into it to make it a palindrome?

There's a nice set of observations we can make to solve this problem. For starters, note that any string of length zero or one is already a palindrome. As a result, if we're asked to add characters to a string of length zero or one, then we don't need to add any characters in at all. We already have a palindrome: That gives us our base case:

Base Case : Any string of length zero or one requires no added characters to make a palindrome.

Let's suppose that, instead, we have two or more characters in our string. For it to be a palindrome, its first and last characters must match one another - otherwise, it's not a palindrome. Lots of other things have to happen for our string to become a palindrome, but certainly if the first and last characters don't match, we're in trouble.

So let's begin by looking at the first and last characters of our string. Either they match, or they don't. In the case where they match, we're in good shape. We won't need to add any specific characters to specifically get those two characters to match one another, At that point. all we have to do is make sure that the remaining characters - the ones on the inside - match one another: That gives us another simple case to consider:

Recursive Case 1: If the first and last character of the string match, pretend they aren't there, and then recursively figure out how many characters are needed to make the middle characters of the string a palindrome.

On the other hand, we may have a string where the first and last characters don't match. That means that we're looking, schematically, at a string that looks something like this:

a - - - - - - - - - - - - - - - - - - b

Now, what has to happen to make this string a palindrome? Well, we're going to have to insert at least one character in order to make things match, since we need to get the first and last character to be the same. There are two different ways that we can do this, though. The first option would be to append an a to the end of the string, like this:

a - - - - - - - - - - - - - - - - - - b a

If we did put an a here, then, using the above rule, we'd say that the first and last characters match, so we could drop the first and last character and look at what's left:

- - - - - - - - - - - - - - - - - - b

The net effect of this is that we've essentially discarded the first character of the string (the original a ) by adding one more character to the string. From here, we'd want to do whatever is best to make the rest of the string a palindrome.

The other option would be to put a b in the front of the string:

b a - - - - - - - - - - - - - - - - - - b

Here, we can match this new b and the old one, giving this:

a - - - - - - - - - - - - - - - - - -

That's required us to add in one character, and then we'd like to (recursively, of course) figure out the fewest number of characters that need to be added to the rest of the string to make it a palindrome.

Overall, this gives us the following rule:

Recursive Case 2: If the first and last characters don't match, then we need to add in one more character, either at the front or the back, to make them match. So try dropping the first character of the string and seeing the best way to proceed from there, then drop the last character of the string and see the best option from there, and take whichever option is better. Don't forget to add 1 for the character we inserted!

Now that you've seen this description, can you map this base case and the two recursive steps onto the code that you've shared with us?

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM