Tarsus (KMP)

Seven is a very cute little very hard girl. She operations on large numbers of very interested in learning after a few days, and finally mastered addition and subtraction, multiplication and division of large numbers. But since she learned JAVA, she felt that large numbers is too simple and too boring, because the use of JAVA in BigInteger large integer class, you can easily add and subtract, multiply and divide large numbers. One day she suddenly found that many of the problems of large numbers of data are laws. These numbers are repeated consists of a number smaller than he, for example, consists of 12 duplicate 121212, 233233, by the repetition of 233, 123 and 1,231,231 is not composed of repeating. Silly little seven finally found a fun, she wants to find large numbers each of which is smaller than the number of repeating its constitution. Algorithms small seven newly recruited ⻔ not solve the problem, so she turned to you.

Entry

Line a positive integer n (n <= 101000000).

Export

Line
if more, the minimum number of output intended to meet the problem of the presence;
if not, outputs -1.

Sample input 

121212

Sample Output 

12

 

#include <bits/stdc++.h>

#define rep(i, l, r) for (int i = l; i <= r; ++ i)

using namespace std;

const int MaxN = 1e7 + 10;

int n;
char s1[MaxN];
int p[MaxN];

int main() {
    scanf("%s", s1 + 1);
    n = strlen(s1 + 1);
    if (n == 0) return 0;
    int j = 0;
    rep (i, 1, (n - 1)) {
        while (j > 0 && s1[j + 1] != s1[i + 1])
            j = p[j];
        if (s1[j + 1] == s1[i + 1])
            j ++;
        p[i + 1] = j;
    }
    if (n % (n - p[n]) != 0 || n == (n - p[n])) printf("-1");
    else rep (i, 1, n - p[n]) cout << s1[i];
    return 0;
}
View Code

 

 

 

Guess you like

Origin www.cnblogs.com/xcfxcf/p/12457410.html