• NOJ Palindromes


    Palindromes

    时间限制(普通/Java) : 1000 MS/ 3000 MS          运行内存限制 : 81920 KByte
    总提交 : 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>

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    c++/oop友元
    c++/oop引用/动态内存/内联函数/函数重载
    CS61A python字符串/常见错误/*args
    c++/oop类和对象/构造函数
    C++/oop指针
    CS61A pyhton高阶函数/lambda
    CS61A PythonBool/if/循环
    c++/oop对象指针/this指针/静态成员/封闭类
    案例实战:B站视频、淘宝购物 短链接跳转
    案例实战:微信抢红包
  • 原文地址:https://www.cnblogs.com/Tobyuyu/p/4965457.html
Copyright © 2020-2023  润新知