hdu2029

来源:互联网 发布:中国知网cnki数据库 编辑:程序博客网 时间:2024/06/05 17:21

Palindromes _easy version

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 29309    Accepted Submission(s): 17809

Problem Description
“回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。请写一个程序判断读入的字符串是否是“回文”。
 

Input
输入包含多个测试实例,输入数据的第一行是一个正整数n,表示测试实例的个数,后面紧跟着是n个字符串。
 

Output
如果一个字符串是回文串,则输出"yes",否则输出"no".
 

Sample Input
4levelabcdenoonhaha
 

Sample Output
yesnoyesno
注释:可以用栈来解决;
代码:
#include <iostream>#include<cstdio>#include<cstring>#include<stack>using namespace std;stack<char> s;int main(){    int n;    while(~scanf("%d",&n))    {        while(n--)        {            char s1[105];        getchar();        scanf("%s",s1);        int len=strlen(s1),j=0;        for(int i=0;i<len;i++)            s.push(s1[i]);       for(j=0;j<len;j++)       {            char c=s.top();            if(s1[j]!=c)            {                break;            }            else                s.pop();       }        if(j==len)            printf("yes\n");        else            printf("no\n");        while(!s.empty())               s.pop();        }    }    return 0;}
0 0
原创粉丝点击