[Notes for brushing questions] Structure memory alignment example + statistical palindrome

1. Example of structure memory alignment

topic:

There are two structures below:

struct One
{
    double d;
    char c;
    int i;
}
struct Two
{
    char c;
    double d;
    int i;
}

In the case of #pragma pack(4) and #pragma pack(8), what are the sizes of the structures ?

analyze:

        Calculating the size of a structure in C/C++ involves memory alignment issues. (For details, please refer to my blog: structure, bit segment, union, enumeration-related memory knowledge (c language)_百海啦的博客-CSDN博客

        Here is a brief summary of the steps to solve this type of problem:

a. Memory alignment process

        1. First, the starting point is set to 0.

        2. Each variable offset corresponds to the number of bytes of its own type size.

        3. The subscript of the starting position of each variable must be an integer multiple of min ( variable byte size , default number of aligned bytes) , if it is insufficient, it needs to be filled forward.

b. Total size after memory alignment

        After each variable is aligned, the total statistical size must be an integer multiple of min ( the largest type size in the variable , the default number of aligned bytes) , and the gap is filled forward.

answer:

        1. If it is #pragma pack(4), it means that the default alignment bytes set is 4.

        For structure One, the alignment process is as follows:

        So the final size is 16byte. Similarly, it can be calculated that the size of Two is 16.

        2. If it is #pragma pack(8), it means that the default alignment bytes set is 8.       

        For the structure Two, the alignment process is as follows:

        Therefore, the total size of the structure Two is calculated to be 24 bytes. Similarly, the calculated size of the structure One is 16 bytes.

2. Statistical palindrome

topic:

Link to Niuke.com : Statistical Palindrome_Niuke Question Master_Niuke.com

describe

        "Palindrome" is a string that reads the same in both forward and reverse, such as "level" or "noon", etc. are palindrome strings. Huahua likes this kind of symmetrical palindrome string very much. On her birthday, she got two gifts, String A and String B. Now she is very curious if there is a way to insert string B into string A so that the resulting string is a palindrome. You accept Huahua's request and help her find how many insertion methods can make the new string a palindrome. If the insertion position of the string B is different, consider a different approach.
For example:
        A = "aba", B = "b". Here are 4 ways to insert B into A:
        * Before the first letter of A: "baba" is not a palindrome
        * After the first letter 'a': "abba" is a palindrome
        * After the letter 'b' After: "abba" is a palindrome
        * After the second letter 'a' "abab" is not a palindrome
        so the answer that satisfies the condition is 2

Enter a description:

        Each set of input data has two rows. The first line is string A and the second line is string B. The length of the strings is less than 100 and only contains lowercase letters.

Output description:

        Output a number indicating the number of ways to insert string B into string A to form a palindrome

Example 1

Input: aba b

Output: 2

analyze:

        First of all, there is no need to be afraid of body types with many words, because most of them should be nonsense. We just need to grasp the key points: 1. Insert string B into string A (the position can be different), 2. Make the new string a palindrome.

        Clarify the thinking and this problem will be easily solved. We first insert string B into different positions of string A. After each insertion, we judge whether it is a palindrome string (reading from front to back is equal to reading from back to front), and count once if it is, otherwise we do not count. For different positions, there should be size(A) + 1 spaces according to the number of characters in string A.

        For the example, we have the following problem-solving process:

answer:

        Because there are multiple insertions of string A to detect palindrome strings, we need to use temporary objects for storage when encoding and implementing. Judging the palindrome string can be judged by comparing whether the characters are equal from the beginning to the end of the double pointer, or can be judged by inverting to judge whether they are equal.

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

bool isPalindrome(string& str)
{
    string temp = str;
    reverse(temp.begin(), temp.end());
    return str ==temp;
}

int main()
{
    string str1, str2;
    getline(cin, str1);
    getline(cin, str2);

    int count = 0;
    for (int i = 0; i <= str1.size(); ++i)
    {
        string str = str1;
        str.insert(i, str2);
        if (isPalindrome(str)) count++;
    }
    cout << count << endl;
}

Guess you like

Origin blog.csdn.net/weixin_61508423/article/details/130529006