编写一个算法利用顺序栈判断一个字符串是否是对称串。所谓对称串是指从左向右读和从右向左读的序列相同。
输入格式:
输入一个无空格的字符串。
输出格式:
如果该字符是对称的,输出yes,否则输出no。
输入样例:
在这里给出一组输入。例如:
abba
输出样例:
在这里给出相应的输出。例如:
yes
输入样例:
在这里给出一组输入。例如:
abcd
输出样例:
在这里给出相应的输出。例如:
no
ACcode:
//方法一
#include <bits/stdc++.h>
using namespace std;
int n , m , sum;
int x[3] , y , z;
int main()
{
string s;
cin>>s;
string ss=s;
reverse(ss.begin(),ss.end()); //直接翻转字符串
cout<<(ss==s?"yes":"no")<<endl;
return 0;
}
//方法二:
#include <bits/stdc++.h>
using namespace std;
int n , m , sum;
int main()
{
string s;
cin>>s;
int i=0 , j=s.length()-1;
while(i!=j && i<j) 从两边向中间匹配
{
if(s[i]!=s[j]) {cout<<"no";exit(0);}
i++;j--;
}
cout<<"yes";
return 0;
}