当前位置:网站首页>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;
}
边栏推荐
- Skills of writing test cases efficiently
- Restful design method
- congratulate! The first dragon lizard community annual outstanding contribution award is announced. Check it now
- Business based precipitation component = & gt; manage-table
- Why are more and more people studying for doctors? Isn't it more and more difficult to graduate a doctor?
- Restcloud ETL extracting dynamic library table data
- C language - structure II
- How about China Power Investment Xianrong futures? Is it safe to open futures accounts?
- How to create simple shapes in illustrator 2022
- High quality defect analysis: let yourself write fewer bugs
猜你喜欢

Top ten popular codeless testing tools

Ten excellent business process automation tools for small businesses

(Video + graphics) introduction to machine learning series - Chapter 11 support vector machines

Architecture decryption from distributed to microservice: several common microservice architecture schemes

Ten software development indicators for project managers

Five skills of selecting embedded programming language
Millions of dollars worth of NFT were stolen in the attack, and Google issued an emergency warning to 3.2 billion users worldwide | February 21 global network security hotspot

Digital transformation informatization data planning and technology planning

How do yaml files and zmail collide with the spark of the framework, and how can code and data be separated gracefully?

如何在 R 中使用 Fisher 的最小显着性差异 (LSD)
随机推荐
How to create simple shapes in illustrator 2022
Six configuration management tools that administrators must know
The country has made a move! Launch network security review on HowNet
Ten software development indicators for project managers
Rapidssl getting started SSL certificate
How can an enterprise successfully complete cloud migration?
Bigdecimalavoiddoubleconstructorrule: do not directly use the double variable as a parameter to construct BigDecimal
C language - structure II
Introduction to yottastore, a new generation of storage engine for VPC TCE cos
Gateway solves cross domain access
Three layer switching experiment
持续助力企业数字化转型-TCE获得国内首批数字化可信服务平台认证
How about China Power Investment Xianrong futures? Is it safe to open futures accounts?
Three indicators to help you measure the effectiveness of digital transformation
It is often blocked by R & D and operation? You need to master the 8 steps before realizing the requirements
面试算法 - 字符串问题总结
Regression testing strategy for comprehensive quality assurance system
About swagger
What makes data analysts good- Cassie Kozyrkov
variable