[code]判断周期串

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include<stdio.h>#include<string.h>int main(){ char word[100]; scanf( "%s",word); int len=strlen(word); for(int i=1;i<=len;i++) if(len%i==0) { int ok=1; for(int j=i;j<len;j++) if(word[j]!=word[j%i]) { ok=0; break; } if(ok){printf("%d\n" ,i);break;} } return 0;} |
//两层for循环,
第一层是字符串周期数的循环,从1:到字符串总长
第二层以上一层确定的周期数,找到第二个周期往后的字符串依次比较,是否相等
浙公网安备 33010602011771号