题解 | #密码截取#

密码截取

http://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1

#include<stdio.h>
#include<string.h>
int main()
{
    char str[2500];
    scanf("%s", str);
    int len = strlen(str);
    int max = 0;
    for(int i=0; i<len; i++)  //abba型
    {
        int cnt = 0;
        if(str[i] == str[i+1])
        {
            for(int j=0; j<len; j++)
            {
                if(str[i-j]==str[i+j+1] && i-j>=0 && i+j+1<len)
                    cnt++;
                else
                    break;
            }
        }
        max = max > 2*cnt ? max : 2*cnt;
    }
    for(int i=0; i<len; i++)  //abba型
    {
        int cnt = 0;
        if(str[i-1] == str[i+1])
        {
            for(int j=0; j<len; j++)
            {
                if(str[i-j-1]==str[i+j+1] && i-j-1>=0 && i+j+1<len)
                    cnt++;
                else
                    break;
            }
        }
        max = max > 2*cnt+1 ? max : 2*cnt+1;
    }
    if(max > 0)
        printf("%d", max);
    else
        printf("1");
    return 0;
}
全部评论

相关推荐

喜欢核冬天的哈基米很想上市:会爆NullPointerException的
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-24 16:03
欲挽天倾:专业毫无意义的 找工作都是看学校title的
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务