URAL 1423. String Tale

1. 题目


1423. String Tale

Time limit: 1.0 second
Memory limit: 64 MB


I want to tell you a story. Not entirely, but only the very beginning, because the ending of this story became a legend of programming—as well as its heroes.
When computers were large, when trees were small, when the sun shined brighter… Once upon a time there were Three Programmers. I doubt whether they participated in any programming contests, because there were no contests at that ancient time. There was neither ACM ICPC nor Timus Online Judge. But there were Three Programmers.


One day Three Programmers invented an amusing game to train memory and mental faculties. The First Programmer thought out a string S which was N characters long and passed it to the Second and the Third Programmers. The Second Programmer executed X (0 ≤ X < N) successive cycle shifts (a cycle shift is a transfer of the last character of the string to the beginning of this string) with this string. As a result of these operations a string T was produced, and the Second Programmer passed it to the Third Programmer. A task of the Third Programmer was to find the number X or make sure that the Second Programmer was mistaken, because the string T could not be produced from the string S via cycle shifts.


The first line contains the integer number N (1 ≤ N ≤ 250000). The second line contains the string S. The third line contains the string T. Each string has length N and may contain any ASCII characters with codes from 33 to 255.


If the string T can be produced from the string S via cycle shifts you should output the desired number X, otherwise you should output “−1”. If the problem has several solutions, you may output any of them.


input output


Let us consider the strings S = “abracadabra” and T = “racadabraab”. The string T can be produced from the string S via 9 cycle shifts: “abracadabra” > “aabracadabr” > “raabracadab” > “braabracada” > “abraabracad” > “dabraabraca” > “adabraabrac” > “cadabraabra” > “acadabraabr” > “racadabraab”
Problem Author: Nikita Rybak, Ilya Grebnov, Dmitry Kovalioff
Problem Source: Timus Top Coders: First Challenge

2. 思路



3. 代码

#include <cstdio>
#include <cstring>

const int MAX_LENGTH = 250001;

void solveP9d_StringTale();
int kmp(char target[], char pattern[], int table[]);
void buildTable(char pattern[], int table[]);

int main() {
    return 0;

char target[MAX_LENGTH*2];
char pattern[MAX_LENGTH];
int table[MAX_LENGTH];
void solveP9d_StringTale() {
    int n;
    scanf("%d", &n);
    scanf("%s", target);
    scanf("%s", pattern);

    int targetLen = strlen(target);
    memcpy(target + targetLen, target, targetLen);
    target[targetLen + targetLen] = '\0';

    buildTable(pattern, table);
    int result = kmp(target, pattern, table);
    result = result > 0 ? (targetLen - result) : result;
    printf("%d\n", result);

int kmp(char target[], char pattern[], int table[]) {
    int m = 0, i = 0, targetLen = strlen(target), patternLen = strlen(pattern);

    while (m + i < targetLen) {
        if (pattern[i] == target[m + i]) {
            if (i == patternLen - 1) {
                return m;
        } else {
            if (table[i] > -1) {
                m = m + i - table[i];
                i = table[i];
            } else {
                i = 0;

    return -1;

void buildTable(char pattern[], int table[]) {
    int pos = 2, cnd = 0, len = strlen(pattern);
    table[0] = -1;
    table[1] = 0;

    while (pos < len) {
        if (pattern[pos - 1] == pattern[cnd]) {
            table[pos] = cnd;
        } else if (cnd > 0) {
            cnd = table[cnd];
        } else {
            table[pos] = 0;