刷题:Palindromes

tech2024-11-01  19

题目:Palindromes 题目链接:https://vjudge.net/problem/UVA-401 题解:水题—字符串处理 只需要分别判断镜像串和回文序列 需要注意一个问题,回问序列不需要判断最中间那个, 镜像串需要判断中间那个,满足镜像串的条件。 代码: #include #include #include using namespace std; string s; int fl,fl2; int middle,len; char ch[]={“AEHIJLMOSTUVWXYZ12358”}; char ch1[]={“A3HILJMO2TUVWXY51SEZ8”}; bool pali() { for(int i=0;i<middle;i++) if(s[i]!=s[len-i-1]) return false; return true; } bool mir() { for(int i=0;i<=middle;i++) { int j; for(j=0;ch[j]!=’\0’;j++) if(ch[j]==s[i]) break; if(ch[j]==0) return false; if(ch1[j]!=s[len-i-1]) return false; } return true; } int main() { while(cin>>s) { len=s.length(); middle=len/2; fl=0; fl2=0; if(pali()) fl=1; if(mir()) fl2=1; if(!fl&&!fl2) cout<<s<<" – is not a palindrome."; if(fl&&fl2) cout<<s<<" – is a mirrored palindrome."; if(fl2&&!fl) cout<<s<<" – is a mirrored string."; if(fl&&!fl2) cout<<s<<" – is a regular palindrome."; cout<<endl<<endl; } return 0; }

最新回复(0)