Three ways to judge palindrome

First, let’s first introduce what a palindrome is:


This is a palindrome, in simple terms, it is symmetrical before and after.

First, introduce the first method: reverse the sequence of numbers, and then judge whether the numbers after the reverse order are exactly the same as the numbers after the positive order. If they are exactly the same, it is a palindrome.

So, how to reverse this string of numbers: It is to use a stack to reverse the order. Take advantage of the first-in-last-out nature of the stack.


code show as below:

#include<bits/stdc++.h>
using namespace std;
int a[9999];
int n;
stack < int > q; //Define a stack structure to output the entire array in reverse order
class heat
{
public:
    void ruzhan() // Push operation, using the first-in-last-out feature of the stack, can make a string of numbers given by the user output in reverse order
    {
        int i=0;
        for(i=0;i<n;i++)
            q.push(a[i] );
    }
public:
    bool hexin() //Core code: Using the properties of the palindrome structure, a string of numbers (strings) in positive and reverse order is exactly the same, which means it is a palindrome
    {
        for(int i=0;i <n;i++)
        {
           if(a[i]!=q.top()) //Normal access to the array is in positive order, and access through the stack is in reverse order. As long as there is a difference between the positive order and the reverse order, return false directly
                return false;
           q.pop();
        }
        return true;//全部一样  返回true
    }
};
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];
    heat p;
    p.ruzhan();
    cout<<p.hexin()<<endl;

}

The second method:

Only the second half of the array is reversed using the stack structure, and then compared with the first half of the array. If all are equal, it is proved to be a palindrome sequence, otherwise, it is not a palindrome sequence.

code show as below:

#include<bits/stdc++.h>
using namespace std;
int a[9999];
int n;
int fast=0; //The fast pointer takes two steps each time
int slow=0; //The slow pointer takes one step each time After the fast pointer is finished, the slow pointer is exactly at the midpoint of the array
stack < int > q; //Define a stack structure to output the second half of the array in reverse order
class heat
{
public:
    void fastandslow() //This function is used to Let the fast-full pointer walk on the array, and finally determine the position of the array pointed to by the slow pointer
    {                 //Note that when the number of numbers is even, we require the slow pointer to stop at the last two numbers in the middle while the previous
        while (fast+2<n)
        {
            slow+=1;
            fast+=2;
        }
    }
public:
    void ruzhan() //Push the numbers in the second half of the array into the stack, in order to output these numbers in reverse order
    {
            for(int i=slow +1;i<n;i++)
                q.push(a[i]);
    }
public:
    bool hexin() //Core code: Using the properties of the palindrome structure, the positive order of the first half of a string of numbers (string) is exactly the same as the reverse order of the second half, just The description is a palindrome
    {              
        if(slow==n/2) //If the total number of numbers is odd, execute the following code
        {
            for(int i=0;i<slow;i++)
            {
                if(a[i] !=q.top()) //Normal access to the array is in positive order, and access through the stack is in reverse order. As long as there is a difference between the positive order and the reverse order, return false directly
                    return false;
               q.pop();
            }
            return true; // All the same return true
        }
        else //If the number of numbers is odd, execute the following code
        {
            for(int i=0;i<=slow;i++)
            {
                if(a[i]!=q.top()) //Normal access to the array is in positive order, and access through the stack is in reverse order. As long as there is a difference between the positive order and the reverse order, return false directly
                    return false;
               q.pop();
            }
            return true; //all the same return true
        }
    }
};
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];
    heat p;
    p. fastandslow();
    p.ruzhan();
    cout<<p.hexin()<<endl;

}

The third method is similar to the second method, except that the third method does not need to use the stack to reverse the order, and directly reverse the order in the array.

code show as below:

#include<bits/stdc++.h>
using namespace std;
int a[9999];
int n;
int fast=0; //The fast pointer takes two steps each time
int slow=0; //The slow pointer takes one step each time After the fast pointer is finished, the slow pointer is exactly at the midpoint of the array
void swap(int i,int j) //Swap function
{
    int temp=a[i];
    a[i]=a[j];
    a[j] =temp;
}
class heat
{
public:
    void fastandslow() //This function is used to make the fast-full pointer move on the array, and finally determine the position of the array pointed to by the slow pointer
    {                 //Note, the number of times When it is an even number, we require the slow pointer to stop at the last two numbers in the middle
        while(fast+2<n)
        {
            slow+=1;
            fast+=2;
        }
    }
public:
    void hounixu()//Through the fastandslow() function, slow has already pointed to the middle position of the array. At this time, it is necessary to reverse the order of the numbers after the slow position, and then judge whether the reversed number is equal to the number in front of the slow.
    {             //If they are equal, then it is a palindrome, otherwise it is not a palindrome
        int frist=slow+1; //The number at the first position after the slow position pointed to by frist end
        =n-1; is the last number of the array
        while (frist<end) //let frist++ and end-- to exchange the number on the array to achieve the reverse order
        {
            swap(frist,end);
            end-=1;
            frist+=1;
        }
    }
public:
    bool hexin()
    {
        if(slow==n/2) //If the total number of numbers is odd, execute the following code
        {
            for(int i=0,j=slow+1;i<slow;i++,j++)
                if(a[i]!=a[j])//The number in front of the slow position is accessed in positive order, and the number after the slow position is accessed in reverse order. As long as there is a difference between the positive order and the reverse order, return false directly
                    return false;
            return true; //All the same return true
        }
        else //If If the number of numbers is odd, execute the following code
        {
            for(int i=0,j=slow+1;i<=slow;i++,j++)
                if(a[i]!=a[j]) //slow position The previous number is a positive order access, and the number after the slow position is a reverse order access. As long as there is a difference between the positive order and the reverse order, return false directly
                    . return false;
            return true; //all the same return true
        }
    }
};
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];
    heat p;
    p.fastandslow();
    p.hounixu();
    cout<<p.hexin()<<endl;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325763809&siteId=291194637