Description
试写一个算法,识别依次读入的一个以@为结束符的字符序列是否为形如‘序列1&序列2’模式的字符序列。其中序列1和序列2中都不含字符‘&’,且序列2是序列1的逆序列。输出YES或者NO。
Input
a+b&b+a
Output
YES
Sample Input
1+3&3-1
Sample Output
NO
#include <stdio.h>
#include<string.h>
#include <stack>
#include <iostream>
using namespace std;
int main()
{
stack <char> x;
char s[100];
int i;
gets(s);
for(i=0; i<strlen(s); i++)
{
if(s[i]!='&')
{
x.push(s[i]);
}
if(s[i]=='&')
break;
}
i++;
for(; i<strlen(s); i++)
{
if(s[i]==x.top())
{
x.pop();
}
}
if(x.empty())
printf("YES");
else printf("NO");
return 0;
}