Palindromes
总提交 : 65 测试通过 : 36
题目描述
Write a program that determines if each input string is a palindrome. A palindrome is a
string that reads exactly the same in both forward and reverse directions. For something
to be considered a palindrome, it must be at least 1 character long. For the purposes of
your program, ignore any characters that are not letters, as well as spaces when
determining if a string is a palindrome.
输入
The first line of input contains an integer N that indicates the number of test strings to
follow. On each subsequent line there will be a single test string. Here is a sample:
输出
For each test string, output "yes" if the string was a palindrome, and "no" if it was not a
palindrome. Remember: Ignore any characters that are not letters, as well as spaces.
样例输入
5
able ##was I, e****re I s.aw $Elba
this is not a palindrome
A man, a plan, a canal, Panama
another random string
Sator Arepo Tenet Opera Rotas
样例输出
yes
no
yes
no
yes
分析:判断输入的字符串是否回文,仅考虑字符串中的字母,我们可以将输入字符串中的字母存在另一个数组里进行判断。
实现代码:
<span style="font-size:12px;">#include<iostream> #include<cstdlib> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int T; int main() { // freopen("data.in","r",stdin); scanf("%d",&T); getchar(); lp:while(T--) { char a[200000],b[200000]; a[0]=' '; b[0]=' '; gets(a); int cnt=0; for(int i=0;i<strlen(a);i++) { if((a[i]>='A'&&a[i]<='Z')||(a[i]>='a'&&a[i]<='z')) b[cnt++]=a[i]; } for(int i=0,j=cnt-1;i<=j;i++,j--) { if(b[i]!=b[j]&&b[i]!=b[j]+32&&b[i]+32!=b[j]) { printf("no "); goto lp; } } printf("yes "); } }</span>
版权声明:本文为博主原创文章,未经博主允许不得转载。