C - String Tale (SDUT 算法训练 -kmp)

Background

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.

Problem

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.
Input
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.
Output
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.
Example
inputoutput
11
abracadabra
racadabraab
9

Notes

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”


#include<iostream>
#include<cstring>
using namespace std;
const int  maxn=1000005;
char str[maxn],ptr[maxn];
char son[maxn];
int net[maxn];
int n;
void getnext()
{
    int j=0,k=-1;
    net[0]=-1;
    while(j<n)
    {
        //只要net数组的下标比子串的长度小
        //循环就得继续
        if(k==-1||son[j]==son[k])
        {
            //如果k==-1意思是第一个进行初始化
            j++;
            k++;
            net[j]=k;
        }
        else
            k=net[k];
    }
}
int kmp()
{
    int i,j;
    i=j=0;
    getnext();
    while(i<n*2)
    {
        if(j==-1||ptr[i]==son[j])
        {
            i++;
            j++;
        }
        else
            j=net[j];
        if(j==n)
            return i;
    }
    return -1;
}
int main()
{
    cin>>n;
    cin>>str;
    cin>>son;
    strcpy(ptr,str);
    strcat(ptr,str);
    int ans=kmp();
    if(ans==-1)
        cout<<-1<<endl;
    else
    {
        if(ans==n)
            cout<<0<<endl;
        else
            cout<<n*2-ans<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值