题目描述
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.
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.
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;
}