原题链接:https://acm.hznu.edu.cn/OJ/problem.php?id=1840
Description
zz,cjc发现他自己的名字是一个回文串,于是他特别喜欢回文串。
现在,他们已经不满足于找到回文串,他们想要创造回文串(两个小学生)。他们发现,有些字符串在首或者尾加上一个字符就能变成一个回文串。他们已经找了许多的字符串,请你帮忙看下其中哪些是符
Input
多组数据输入,每行包含一串小于200个字符的字符串。
合要求的字
Output
如果是符合要求的字符串,则输出”zz happy!”;
如果不是符合要求的字符串,则输出”zz cry”。
Samples
input Copy
zzlovezjyjzevolz zzlovezjyzevozz
output Copy
zz happy! zz cry!
解题思路:写一个判断回文串的函数,分别判断去掉首字符或者去掉最后一个字符的字符串是否是回文串即可。
ac代码:
#include <bits/stdc++.h> using namespace std; bool isPalindrome(string str) { int length = str.size();//字符串长度 int mid = length/2;//字符串中间字符的位置 int i, j; for( i=length-1,j=0; j<mid; i--,j++){ if(str[i]!=str[j]) return false; } return true; } int main() { ios::sync_with_stdio(false);//关闭同步流 cin.tie(0); cout.tie(0); string s1; while(cin >> s1) { int len=s1.size(); string s2=s1.substr(1);//去掉第一个字符 // cout << s2 << endl; string s3=s1.substr(0,len-1);//去掉最后一个字符 // cout << s3 << endl; if(isPalindrome(s2)) { cout << "zz happy!" << endl; }else if(isPalindrome(s3)) { cout << "zz happy!" << endl; }else cout << "zz cry" << endl; } return 0; }