Algorithm questions of strings from shallow to deep (vs: chatGPT as algorithm)

background

As the saying goes, learn the new by reviewing the old. The effect of chatGPT is amazing! It is simply the effect of crushing. But there is still hope, pick it up first, and then innovate. Understand first, then go beyond.

ps: Brush up the algorithm question ideas for the last time. By the way, feel the magic of the big model based on chatGPT3.5.

String Basics

Each string of C/C++ ends with '\0', so that it is very convenient to find the last end of the string. Due to this feature, each string has an overhead of an extra character, and a little carelessness will cause the string to go out of bounds. As follows, in order to correctly copy 0123456789 to the array str, you need to declare an array with a length of 11 bytes.

char str[10];
strcpy(str,"0123456789");

String Arrays and String Pointers

To save memory, C/C++ puts constant strings in a separate memory area. When several pointers are assigned to the same constant string, they actually point to the same memory address. But with arrays initialized with constant memory, the situation is different.

Run the following code, what will be the result?

int main()
{
    char str1[]="hello world";
    char str2[]="hello world";

    char* str3="hello world";
    char* str4="hello world";

    if(str1==str2)
        printf("str1 and str2 are same.\n");
    else
        printf("str1 and str2 are not same.\n");

    if(str3==str4)
        printf("str3 and str4 are same.\n");
    else
        printf("str3 and str4 are not same.\n");

    return 0;    
}

str1 and str2 are two string arrays, we will allocate two spaces with a length of 12 bytes for them, and copy the contents of "hello world" to the arrays respectively. These are two arrays with different initial addresses, so str1 and str2 have different values.

str3 and str4 are two pointers, we don't need to allocate memory for them to store the content of the string, but just point them to the address of "hello world" in memory. Since "hello world" is a constant string, there is only one copy of it in memory, so str3 and str4 have the same value.

Algorithm question: replace spaces

Question: Please implement a function to replace each space in the string with "%20". For example, input "We are happy.", then output "We%20are%20happy.".

Idea: Seeing this topic, we should first think of the original space character, which becomes the three characters '%', '2', '0' after replacement, so the string will become longer. If the replacement is performed on the original string, it is possible to overwrite and modify the memory behind the string. If we create a new string and replace it on the new string, then we can allocate enough memory ourselves. Therefore, it is necessary to clarify the needs of the questioner first. If you want to replace the original string, and ensure that there is enough free memory behind the input string.

A solution with a time complexity of O(n^2)

The most intuitive way to do this is to scan the string from beginning to end, replacing each time a space character is encountered. Since 1 character is replaced by 3 characters, we must move all the following characters back by 2 bytes, otherwise 2 characters will be overwritten. Suppose the length of the string is n. For each space character, O(n) characters need to be moved, so for a string containing O(n) spaces, the total time efficiency is O(n^2).

A solution with a time complexity of O(n)

You can first traverse the string once and count the total number of spaces in the string. Then start copying and replacing from the back of the string. Prepare two pointers: p1 and p2. P1 points to the end of the original string, and P2 points to the end of the replaced string.

Next move the pointer P1 forward, and copy the characters it points to the position pointed to by P2 one by one until the first space is encountered. After encountering the first space, move p1 forward one space, and insert the string "%20" before P2. Since the length of "%20" is 3, move P2 forward 3 spaces at the same time. In this way, P1 is then copied forward until it encounters the second space. When P1 and P2 point to the same position, it means that all spaces have been replaced.

All characters of this algorithm are copied (moved) only once, so the time complexity of this algorithm is O(n).

/*length 为字符数组str的总容量,大于或等于字符串str的实际长度*/
void ReplaceBlank(char str[], int length)
{
    if(str == nullptr && length <= 0)
        return;

    /*originalLength 为字符串str的实际长度*/
    int originalLength = 0;
    int numberOfBlank = 0;
    int i = 0;
    while(str[i] != '\0')
    {
        ++ originalLength;

        if(str[i] == ' ')
            ++ numberOfBlank;

        ++ i;
    }

    /*newLength 为把空格替换成'%20'之后的长度*/
    int newLength = originalLength + numberOfBlank * 2;
    if(newLength > length)
        return;

    int indexOfOriginal = originalLength;
    int indexOfNew = newLength;
    while(indexOfOriginal >= 0 && indexOfNew > indexOfOriginal)
    {
        if(str[indexOfOriginal] == ' ')
        {
            str[indexOfNew --] = '0';
            str[indexOfNew --] = '2';
            str[indexOfNew --] = '%';
        }
        else
        {
            str[indexOfNew --] = str[indexOfOriginal];
        }

        -- indexOfOriginal;
    }
}

Related topic: Two sorted arrays are sorted and merged on one of them

Description: Two sorted arrays A1 and A2 with enough free space at the end of A1 to hold A2. Please implement a function to insert all the numbers of A2 into A1, and all the numbers are sorted.

Answer idea:

If A1 and A2 are compared and inserted from front to back, then there will be multiple copy numbers. A better way is to compare the numbers in A1 and A2 from the end to the beginning, and copy the larger number to A1 Suitable location. Thereby reducing the number of moves and improving efficiency.

Easter eggs: chatGPT algorithm

The same problem was thrown to chatGPT3.5. It first gave the easiest idea to implement, and then after two rounds of manual reminders, it continued to optimize and obtained the optimal solution we mentioned above.

Comment on chatGPT's answer:

The answers of the first two rounds of chatGPT are correct. But the answer in the third round is wrong .

For example, when it is found that arr1[i] is greater than arr2[j], in order to reduce the number of times arr1 moves backward, it deliberately does not let the elements in arr1[i+1:] move to the right, but starts from arr1[i+ 1:] Find a number less than or equal to arr2[j]. Here, because arr1 is an ordered array, the subsequent numbers cannot be smaller than the previous ones. Obviously, the logic of chatGPT is confused.

In fact, in the third round of optimization, because my network is not stable, chatGPT regenerated 3 answers. The above shows the relatively complete reply generated for the third time, but I recorded the process of its previous 2 answers. Although It is not written in full, but the idea is the double pointer we mentioned above, the method of comparing and inserting two arrays from back to front. It may have asked it to regenerate the answer many times, which misled it. It thought that the answer was not well thought out.

The first two times were interrupted because of network problems and only half of the answers were generated. as follows:

1st generation answer

The second generated answer:

Seeing this, how do you feel?

Guess you like

Origin blog.csdn.net/u010420283/article/details/129678137