题解 | 密码截取
密码截取
https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
#include <stdio.h> #include<string.h> int main() { int out=0,count=0,len=0; char str[2501]={}; scanf("%s",str); len=strlen(str); //模式1,aba, 以str[i]为基点,判断其左右两侧+j-j的数据是否一致,一致则count+2,进入j+1 for(int i=1;i<len;i++){ count=1; for(int j=1;j<((len+1)/2);j++){ if(str[i+j]==str[i-j]){ count+=2; continue; } else{ break; } } if(count>out)out=count; } //模式2,abba,同理,先判断i和i+1,然后i+j和i+1+j for(int i=0;i<len;i++){ count=0; for(int j=0;j<len/2;j++){ if(str[i-j]==str[i+1+j]){ count+=2; continue; } else{ break; } } if(count>out)out=count; } printf("%d",out); return 0; }