/********************************
* 程序名称:栈回文串
* 作 者:tiaya@qq.com
* 来 源:B栈Tracy老师
* 开发时间: 2020 - 1 - 11
*******************************/
#include <iostream>
using namespace std;
string s;
//find()
bool find(string a) {
int la, mid,top, next;
la = a.length(); //s.size()
mid = la / 2;
top = -1;
if(la % 2 == 0)
next = mid;
else {
next = mid + 1;
}
for(int i=0; i < mid; i++) {
top ++;
s[top] = a[i]; //push
}
for(int i=next; i<la; i++) {
if(a[i] == s[top]) {
top --;
}
}
if (top == -1) {
return true;
} else {
return false;
}
}
/*main()*/
int main(int argc, char const *argv[])
{
/* code */
string str;
cout << "输入字符串:";
cin >> str;
if (find(str)) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}
测试1:
Please input string:helloello
No
测试2:
Please input string:hellolleh
Yes
网友评论