若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。


  • 运维

  • 前端

  • 编程

  • 随笔

  • hust-oj

1641: Palindromes

发表于 2017-10-06   |   分类于 HUSTOJ   |   阅读次数 1,139

题目描述

A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDEDCBA" is a palindrome because it is the same when the string is read from left to right as when the string is read from right to left.

Now give you a string S, you should count how many palindromes in any consecutive substring of S.

输入

There are several test cases in the input. Each case contains a non-empty string which has no more than 5000 characters.

Proceed to the end of file.

输出

A single line with the number of palindrome substrings for each case.

样例输入

aba
aa

样例输出

4
3

参考代码

#include <stdio.h>
int main() 
{
    int i,j,k,g,n,h;
    char a[5005];
    while(scanf("%s",a)!=EOF) 
    {
        k=strlen(a);
        h=k;
        for (i=1; i<k; i++) 
        {
            for (n=0; n<k; n++) 
            {
                g=n+i;
                if (g>k)
                                    break;
                for (j=n; j<=(n+i); j++) 
                {
                    if (a[j]!=a[g])
                                            break;
                    if (j==(n+i))
                                            h++;
                    g--;
                }
            }
        }
        printf("%dn",h);
    }
    return 0;
}

解析

暂无

hustoj

发表评论 取消回复

电子邮件地址不会被公开。 必填项已用*标注

*
*


hoxis wechat
著作权归作者所有
站点更新说明
  • 文章目录
  • 站点概览
若是凉夜已成梦

若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。

1904 日志
6 分类
12 标签
RSS
weibo github twitter facebook

友情链接

Skip 原站点 Dreams孤独患者
© 2017 若是凉夜已成梦
Powered by WordPress | 已运行
Theme By NexT.Mist