回文字符串【华中科技大学考研机试题 算法标签】

回文字符串

给出一个长度不超过 1000 的字符串,判断它是不是回文(顺读,逆读均相同)的。

输入格式
输入包含多组测试数据。

每组数据占一行,包含一个有小写字母构成的字符串。

输出格式
每组数据输出一行一个结果,如果是回文字符串则输出 Yes!,否则输出 No!。

数据范围
输入最多包含 100 组数据。

输入样例:
hellolleh
helloworld
输出样例:
Yes!
No!

代码

点击查看代码
#include<bits/stdc++.h>
using namespace std;

bool test(string t){
    for(int i = 0,j = t.size() - 1; i <= j; i ++, j -- ){
        if(t[i] != t[j])return 0;
    }
    return 1;
}

int main(){
    string s;
    while(cin >> s){
        if(test(s))cout << "Yes!" << '\n';
        else cout << "No!" << '\n';
    }
}
posted @ 2023-03-07 15:40  Keith-  阅读(16)  评论(0编辑  收藏  举报