HDU 2163 Palindromes

链接 : http://acm.hdu.edu.cn/showproblem.php?pid=2163


还是判断回文,栈,输入“STOP”时停止


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
using namespace std;
int main()
{
    int k=1;
    char str[53];
    while(cin>>str,strcmp(str,"STOP"))
    {
        bool flag=0;
        stack<char> s;
        int len=strlen(str);
        if(len==1)
        {
            cout<<"#"<<k++<<": YES"<<endl;
            continue;
        }
        if(len%2) flag=1;
        for(int i=0; i<len; i++)
        {
            if(flag && i==len/2) continue;
            if(s.empty() || s.top()!=str[i]) s.push(str[i]);
            else s.pop();
        }
        printf(s.empty() ? "#%d: YES\n" : "#%d: NO\n",k++);
    }
    return 0;
}


你可能感兴趣的:(字符串,栈,水题,HDU2163)