怎么判断回文串
什么是回文串? abcba 当字符串是对称结构时,就是回文串。
根据回文串的特点,我们可以很快判断字符串是不是回文串,只需要从头尾各取字符逐个比对,一直比对结束为止,都相等则为回文串(回文串长度大于 2)
#include <iostream>
#include <string>
using namespace std
;
bool isPalindrome(const string
& src
, int begin
, int end
) {
while (begin
< end
) {
if (src
.at(begin
) != src
.at(end
)) {
return false;
}
begin
++;
end
--;
}
return true;
}
int main()
{
std
::string str
;
while (std
::cin
>> str
) {
std
::cout
<< str
<< ":" << isPalindrome(str
, 0, str
.length() - 1) << std
::endl
;
}
return 0;
}
上述代码可以参考用来查找是否是回文字符串,没有将长度为 1 的字符串列为非回文,大家做题的时候可以考虑进去。