题解 | #密码截取#
密码截取
https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
遍历以单个字母展开的、两个相同字母展开的,取最大值。
#define MAX(a,b) (a>b)?(a):(b) int main() { int n; char str[2501]; int max; scanf("%s", str); n = strlen(str); int j, k; max = 0; for(int i=0; i<n; i++) { j = i-1; k = i+1; if(j<0 || k>=n) { max = MAX(max,1); continue; } while(j>=0 && k<n && str[j]==str[k]) { j--; k++; } max = MAX(max, k-j-1); j = i; k = i+1; while(j>=0 && k<n && str[j]==str[k]) { j--; k++; } max = MAX(max, k-j-1); } printf("%d\n", max); }