01:判断字符序列是否对称
-
时间限制:
- 1000ms
- 1000kB
-
描述
-
编写一个算法,判断依次读入的一个以@为结束符的字母序列,是否为形如
‘序列1&序列2’模式的字符序列。其中序列1和序列2中都不含字符‘&’,且序列2是序列1的逆序。又如:
1+2&2+1@ 输出 Right
456&651a@ 输出 Wrong
0123&321@ 输出 Left more
abc&cbad@ 输出 Right more
输入
- 1+2&2+1@ (遇到@结束输入) 输出
- Right 样例输入
-
a-b&b+a@
样例输出
-
Wrong
#include <stdio.h>
#define MaxSize 100
typedef struct stack{
int top;
char elem[MaxSize];
}stack;
int push(char ch, stack*s)
{
if(s->top>=MaxSize)
return MaxSize;
else
{
s->top++;
s->elem[s->top]=ch;
return s->top;
}
}
int pop(char *ch ,stack *s)
{
if(s->top<0)
return -1;
else
{
*ch=s->elem[s->top];
s->top--;
return s->top+1;
}
}
int main()
{
stack *s=(stack*)malloc(sizeof(stack*));
s->top=-1;
char str[MaxSize];
int j,i=0;
char ch;
scanf("%c",&str[i]);
while(str[i]!='@')
{
i++;
scanf("%c",&str[i]);
}
for(j=0;j<i;j++)
{
if(str[j]!='&')
push(str[j],s);
else
break;
}
for(j=j+1;j<i;j++)
{
if(pop(&ch,s)>-1)
{
if(ch!=str[j])
{
printf("Wrong\n");
return 0;
}
}
else
{
printf("Right more\n");
return 0;
}
}
else
printf("Right\n");
}