Original question link:
Http://acm.hust.edu.cn/thx/problem.php? Id = 1010
It is not difficult to understand this question. Generally, the first solution is brute-force solution. However, for ACM, the brute-force solution usually gets TLE, that is, timeout.
This question is actually being investigated by KMPAlgorithm. For the introduction of KMP algorithm, see: http://baike.baidu.com/view/659777.htm
The KMP algorithm is a classic algorithm for string matching. It defines a unique feature vector for each string to match characters one by one, making the complexity of the algorithm from the general O (M * n) changed to O (m + n), greatly shortening the matching time. The key to this question is to find the regularity of feature vectors to quickly obtain the answer.
The source code is as follows:
# Define hust_1010 # ifdef hust_1010 # include <iostream> # include <string. h> # include <stdio. h> using namespace STD; # define n 1000002 char B [N]; int nextx [N]; void getnext (INT next [], char s [], int L) {int I = 1, j = 0; next [1] = 0; while (I <L) {If (j = 0 | s [I] = s [J]) {I ++; j ++; next [I] = J ;} else {J = next [J] ;}} int main () {// freopen ("test \ 1010-input.txt", "r", stdin ); // freopen ("test \ 1010-output.txt", "W", stdout); int Len; int sum; Wh Ile (scanf ("% s", B + 1 )! = EOF) {sum = 0; Len = strlen (B + 1); If (LEN = 1) printf ("1 \ n"); else {getnext (nextx, b, Len); sum = len-nextx [Len]; If (B [Len] = B [Len % sum]) | (LEN % sum = 0 & (B [Len] = B [Sum]) printf ("% d \ n", sum ); elseprintf ("% d \ n", Len) ;}} return 0 ;}# endif