SP13043 题解
原题传送门
这一道题其实就是让我们判断这个字符串是否为回文串(不知道回文串是什么的,可以点这里)。在 C++ STL 中,reverse()函数可以较为方便地做到这一点。
reverse()函数的参数是reverse(beg,end),作用是将区间[beg,end)内的元素在原容器中全部逆序。reverse()函数没有返回值,时间复杂度为 reverse()函数时,须引用头文件algorithm。
更多有关reverse()函数的知识,可以看这里和这里。
代码如下:
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int n;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
string s,x;
cin>>s;
x=s;
reverse(x.begin(),x.end());//x即为逆序后的s
if(s==x){//如果逆序前后的s相等,则说明s是回文串
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;
}
}
return 0;
}