The main idea: give a string to ask it up by how many of the same string composition
If the abababab is made up of 4 AB
Analysis:
The application of the next array in KMP to the minimum cyclic section
For example
Ababab next[6] = 4; That
Ababab
Ababab
The 1~4 bit is the same as the 2~6 bit
So the first two
is equal to 3, 4 bits
3, 4 bits equals 5, 6 bits
......
So if we can divide it, we'll loop to the end.
If not divisible
The last remaining few are not in the loop.
For example
1212121
1212121
The last remaining 1 cannot be equal to the cyclic section
Code:
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 6 Const intMAXN =1000005;7 8 intNEXT[MAXN];9 Ten void Get(Char*s) { One intL =strlen (s); A intj =0, k =-1; -next[0] = -1; - while(J <l) { the if(k = =-1|| S[J] = =S[k]) { -NEXT[++J] = + +K; -}Else { -K =Next[k]; + } - } + } A CharS[MAXN]; at - intMain () { - while(gets (s)) { - if(strcmp (s),".") ==0) { - Break; - } in Get(s); - intAns =1; to intL =strlen (s); + if(l% (l-next[l]) = =0) { -ans = l/(L-next[l]); the } *printf"%d\n", ans); $ }Panax Notoginseng}View Code
poj2406 (KMP next array)