YTU OJ 2241: 相同序列(栈和队列)

来源:互联网 发布:51单片机自学笔记视频 编辑:程序博客网 时间:2024/06/05 12:41

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;}


原创粉丝点击