SDUT OJ 2399 Palindrome

题目描述

A string is said to be a palindrome if it reads the same both forwards and backwards, for example "madam" is a palindrome while "acm" is not. 
Now we want to know how many palindrome strings in the given strings.

输入

The first line of the input contains a single integer T, which indicates number of the given strings.
The next T lines contain one string S, whose length is less than 1000 letters.

输出

One integer indicates the number of palindrome strings.

示例输入

5
ababa
aabb
ab
cccccc
Abcd

示例输出

2

题意:就是统计回文串的个数;

给出两个代码 一个是对半查找,能省一点儿时间


对半查找:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
char s1[1010];
using namespace std;
int main()
{
    int n,i,j;
    int cnt=0;
    int flag;
    scanf("%d",&n);
    for(i=0;i<n;i++){
        flag=0;
        memset(s1,0,sizeof(s1));
        scanf("%s",s1);
        int len=strlen(s1);
        for(j=0;j<=(len-1)/2;j++){
            if(s1[j]!=s1[len-j-1]){
                flag=1;
                break;
            }
        }
        if(!flag)
            cnt++;
    }
    printf("%d\n",cnt);
    return 0;
}

正常做法

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
char s1[1010],s2[1010];
using namespace std;
int main()
{
    int i,j,k,len,n;
    int cnt=0;
    scanf("%d",&n);
    for(i=1; i<=n; i++)
    {
        memset(s2,0,sizeof(s2));
        scanf("%s",s1);
        j=0;
        len=strlen(s1);
        for(k=len-1; k>=0; k--)
        {
            s2[j]=s1[k];
            j++;
        }
        if(strcmp(s1,s2)==0)
            cnt++;
    }
    printf("%d\n",cnt);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值