HDU 2029 Palindromes _easy version

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


栈的运用 //不用栈也可以

当时把奇数和偶数分开写了,有点啰嗦,其实非常简单

#include <iostream>
#include <stack>
#include <cstring>
using namespace std;
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        char str[1000];
        stack<char> s;
        while(!s.empty()) s.pop();
        cin>>str;
        if(strlen(str)%2==0)
        {
            for(int i=0; i<strlen(str); i++)
            {
                if(s.empty()) s.push(str[i]);
                else
                {
                    if(s.top()==str[i]) s.pop();
                    else s.push(str[i]);
                }
            }
        }
        else
        {
            for(int i=0; i<strlen(str); i++)
            {
                if(i==strlen(str)/2) continue;
                if(s.empty()) s.push(str[i]);
                else
                {
                    if(s.top()==str[i]) s.pop();
                    else s.push(str[i]);
                }
            }
        }
        if(!s.empty()) cout<<"no"<<endl;
        else cout<<"yes"<<endl;
    }
    return 0;
}



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