数据结构实验之串一:KMP简单应用

数据结构实验之串一:KMP简单应用
Time Limit: 1000MS Memory Limit: 65536KB

Problem Description
给定两个字符串string1和string2,判断string2是否为string1的子串。

Input
输入包含多组数据,每组测试数据包含两行,第一行代表string1(长度小于1000000),第二行代表string2(长度小于1000000),string1和string2中保证不出现空格。

Output
对于每组输入数据,若string2是string1的子串,则输出string2在string1中的位置,若不是,输出-1。

Example Input
abc
a
123456
45
abc
ddd

Example Output
1
4
-1

Hint

Author
cjx

以下为accepted代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXN 1000004
int next[MAXN];
void get_next(char *p)
{
    int i = 0, j = -1;
    int len = strlen(p);
    next[0] = -1;
    while(i < len-1)
    {
        if(j == -1 || p[i] == p[j])
        {
            i++;
            j++;
            next[i] = j;
        }
        else
            j = next[j];//失配时j的值
    }
}
int kmp(char *s, char *p)
{
    int i = 0, j = 0;
    int len1 = strlen(s);
    int len2 = strlen(p);
    memset(next, 0, sizeof(next));
    get_next(p);
    while(i < len1 && j < len2)
    {
        if(j == -1 || s[i] == p[j])
        {
            i++;
            j++;
        }
        else
            j = next[j];//失配时str2回溯的值
    }
    if(j >= len2)
        return i-len2+1;
    else
        return -1;
}
int main()
{
    char s[MAXN], p[MAXN];
    while(scanf("%s %s", s, p) != EOF)
    {
        printf("%d\n", kmp(s, p));
    }
    return 0;
}


/***************************************************
User name: jk160630
Result: Accepted
Take time: 100ms
Take Memory: 5096KB
Submit time: 2017-02-04 11:09:44
****************************************************/

以下为建议参考的代码,感谢作者的启迪,谢谢。

    #include<bits/stdc++.h>  
    using namespace std;  


    char str1[1000001],str2[1000001];  
    int next[1000001];  


    void get_next()  
    {  
        int i=0;//i是next数组下标  
        next[0]=-1;  
        int j=-1;//j是模板字符串前后缀长度  
        while(str2[i]!='\0')//从第二个字符开始,计算next数组对应的值  
        {  
            if(j==-1||str2[i]==str2[j])//如果相等,下标后移,前后缀长度加1  
            {  
                i++;  
                j++;  
                next[i]=j;  
            }  
            else  
                j=next[j];//失配时j的值  
        }  
    }  


    void kmp()  
    {  
        int i=0,j=0;  
        get_next();  
        int len1=strlen(str1);  
        int len2=strlen(str2);  
        while(i<len1&&j<len2)  
        {  
            if(j==-1||str1[i]==str2[j])  
            {  
                i++;  
                j++;  
            }  
            else  
            {  
                j=next[j];//失配时str2回溯的位置  
            }  
        }  
        if(j>=len2)  
            printf("%d\n",i-len2+1);//输出第一次匹配时的位置  
            else  
                printf("%d\n",-1);  
    }  


    int main()  
    {  
        while(cin>>str1>>str2)  
        {  
            kmp();  
        }  
        return 0;  
    }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值