题解 | #计算某字符出现次数#
密码截取
http://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
1.两个for循环搜索所有可能可能长度的连续的字符串
for(int i=0;i<strlen;i++){
for(int j=strlen-1;j>i; j--){
...
}
}
密码截取
http://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
#include<stdio.h> #include<string.h> int main(){ char s[2500]={0}; scanf("%s",s); int len = strlen(s); int max_v = 1; int f = 0; for(int i=0;i<len;i++){ for(int j=len-1;j>i;j--){ int count = 0; int m,n; if((i+j)%2==1){ m = (i+j)/2; n = (i+j)/2+1; } else{ m = (i+j)/2-1; n = (i+j)/2+1; count = 1; } int x=m,y=n; for(;x>=i;x--,y++){ if(s[x]==s[y]) count = count+2; else break; } if(count>max_v) max_v = count; if(count==len-i){ // len-i最大可能的长度 f = 1; break; } } if(f==1){ break; } } printf("%d",max_v); }
相关推荐