HDOJ 2029 Palindromes _easy version

第一個思路有問題 ios

就是迴文的時候是奇數個的情況 spa

不一樣於判斷括號的問題 code


還是用數組寫吧 ci

#include <iostream>
#include <stdio.h>
#include <stack>
using namespace std;

int main(){
    int t;
    cin>>t;
    stack<char>my;
    char a[100];
    while(t--){
        cin>>a;
        for(int i=1; i<strlen(a); i++){
            if(my.empty()==1)//這個判斷必須有
                my.push(a[i]);
            else{
                if(my.top()==a[i])
                    my.pop();
                else
                    my.push(a[i]);
            }
        }
        if(my.empty()==1)
            cout<<"yes"<<endl;
        else{
            cout<<"no"<<endl;
            while(my.empty()!=1)my.pop();
        }
    }
    return 0;
}
正解

#include <iostream>
using namespace std;

int main(){
    int t,n;
    bool flag;
    char s[100];
    cin>>t;
    while(t--){
        flag=true;
        cin>>s;
        n=strlen(s);
        for(int i=0; i<=n/2-1; i++){
            if(s[i]!=s[n-i-1]){
                flag=false;
                break;
            }
        }
        if(flag)
            cout<<"yes"<<endl;
        else
            cout<<"no"<<endl;
    }
    return 0;
}
相關文章
相關標籤/搜索