题解 | #密码截取#
密码截取
https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
#include<stdio.h>
#include<string.h>
int main()
{
char arr[2500]={0};
scanf("%s",arr);
int len=strlen(arr);
int max=0;
for(int i=1;i<len;i++)//中心扩散法
{
int left=i-1,right=i+1;
int sz=1;
while(right<len&&arr[right]==arr[i])//跳过重复的字符
{
right++;
sz++;
}
while(left>=0&&right<len&&arr[right]==arr[left])//判断是否相同
{
sz+=2;
left--;
right++;
}
if(sz>max)
max=sz;
}
printf("%d",max);
return 0;
}
#include<string.h>
int main()
{
char arr[2500]={0};
scanf("%s",arr);
int len=strlen(arr);
int max=0;
for(int i=1;i<len;i++)//中心扩散法
{
int left=i-1,right=i+1;
int sz=1;
while(right<len&&arr[right]==arr[i])//跳过重复的字符
{
right++;
sz++;
}
while(left>=0&&right<len&&arr[right]==arr[left])//判断是否相同
{
sz+=2;
left--;
right++;
}
if(sz>max)
max=sz;
}
printf("%d",max);
return 0;
}