当前位置:网站首页>Palindrome string (two methods)

Palindrome string (two methods)

2022-06-24 18:30:00 One star accompanies the moon

Palindrome string : A special string that is read forward and read backward

for example :level、abba These two strings are called palindromes

Judging palindrome string , I recommend two methods : Reverse comparison and middle point comparison ( The name is my own , There are some Ugly

One 、 Reverse comparison method

about c++ Students of this is very simple
The string type we entered is located string type , And then use reverse The function can , Then we directly compare str1 And str2 Whether it is equal or not , If it is equal, it means that the input string is a palindrome string .

reverse(str2.begin(), str2.end());

Of course , Input char、int Arrays are also possible , Use it directly reverse function

reverse(str2,str2+strlen(str2));

For not using functions or c Classmate , Then write one of your own reverse function , This is better to achieve , Define a new array , Then reverse it recursively .

void reverse(char * str)
{
    
    int len=strlen(str);
    char *ch=str+len-1;
    while(len>1)
    {
    
        char tmp=*str;
        *str=*ch;
        *ch='\0';       
        reverse(str+1); 
        *ch = tmp;
        len--;
    }
}

Two 、 Median comparison method

In this way, I will use the single chain table of the leading node to store each element , Let's build another stack , We use the stack to store the first half of the data , If the number of data elements is odd , Let's add a special sentence , Do not process this node . When the current node of the linked list is halfway through , We compare the stack top element with the current node , If it's not equal , That means it is not a palindrome string ,, If the top elements of the stack are equal, they are out of the stack , The pointer moves back one bit . If the ending is equal , Then it means palindrome string .
The following code input format is :
String length
character string

for example :
5
abbba

#include<stdio.h>
#include<stdlib.h>
typedef struct node{
    
	char num;
	struct node *next;
}LinkList;
LinkList *init(LinkList *l)
{
    
	l=(LinkList *)malloc(sizeof(LinkList));
	l->next=NULL;
	return l;
}
LinkList *create(int n)
{
    
	int i,x;
	LinkList *l,*tmp,*p;
	l=init(l);
	tmp=l;
	for(i=0;i<n;i++)
	{
    
		scanf("%c",&x);
		p=(LinkList *)malloc(sizeof(LinkList));
		p->num=x;
		p->next=NULL;
		tmp->next=p;
		tmp=tmp->next;
	}
	return l;
}
int cala(LinkList *l,int n)
{
    
	char a[1005];
	int mid=n/2,i=1,na=0;
	LinkList *tmp;
	tmp=l->next;
	while(tmp)
	{
    
		if(mid+1==i&&n%2!=0) 
		{
    
			i++;
			tmp=tmp->next;
			continue;
		}
		else if(i<=mid) a[na++]=tmp->num;
		else if(i>mid)
		{
    
			if(a[--na]!=tmp->num)
			{
    
				return 0;
			}
		}
		i++;
		tmp=tmp->next;
	}
	return 1;
}
int main()
{
    
	LinkList *l;
	int n,i,flag;
	scanf("%d",&n);
	getchar();
	l=create(n);
	flag=cala(l,n);
	if(flag) printf("Yes\n");
	else printf("No\n"); 
	return 0;
}

原网站

版权声明
本文为[One star accompanies the moon]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211333580918.html