sdut oj2125 数据结构实验之串二:字符串匹配(BF与KMP做法)

题目链接:点击打开链接

需要注意的是动态字符数组要自行置串结束的标志‘\0’;

数据结构实验之串二:字符串匹配

Time Limit: 1000MS Memory limit: 65536K

题目描述

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

输入

 输入包含多组数据,每组测试数据包含两行,第一行代表string1,第二行代表string2,string1和string2中保证不出现空格。(string1和string2大小不超过100字符)  

输出

 对于每组输入数据,若string2是string1的子串,则输出”YES”,否则输出”NO”。  

示例输入

abc
a
123456
45
abc
ddd

示例输出

YES
YES
NO

提示

 BF算法

代码实现:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
#define maxsize 300

typedef struct
{
    char *ch;
    int length;
    int stringsize;
}Sqstring;

int initString(Sqstring &S)
{
    S.ch = (char *)malloc(maxsize *sizeof(char));
          ///S.ch = new char[maxsize];
    if(!S.ch)
        return -1;
    S.length = 0;
    S.stringsize = maxsize;
    return 0;
}

void Creat(Sqstring &S, char str[])
{
    int i;
    int len = strlen(str);
    for(i = 0;i < len;i++)
    {
        S.ch[i] = str[i];
        S.length++;
    }
    S.ch[i] = '\0';///置串S的结束标志!!

}

void Index(Sqstring &S1,Sqstring &S2)
{
    int i = 0,j = 0;
    while(S1.ch[i + j] != '\0'&&S2.ch[j] != '\0')
    {
        if(S1.ch[i + j] == S2.ch[j])
            j++;
        else
        {
            i++;
            j=0;
        }
    }
    if(S2.ch[j] == '\0')
        printf("YES\n");
    else
        printf("NO\n");
}

int main()
{
    char str1[110],str2[110];
    Sqstring S1,S2;
    while(~scanf("%s%s",str1,str2))
    {
        initString(S1);
        initString(S2);
        Creat(S1,str1);
        Creat(S2,str2);
        Index(S1,S2);
    }
    return 0;
}

 法二:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
#define maxsize 1010

typedef struct
{
    char *ch;
    int length;
    int stringsize;
}Sqstring;

int initString(Sqstring &S)
{
    S.ch = (char *)malloc(maxsize*sizeof(char ));
    if(!S.ch)
        return -1;
    S.length = 0;
    S.stringsize = maxsize;
    return 0;
}

void Creat(Sqstring &S,char s[])
{
    int i;
    int len = strlen(s);
    for(i = 0;i < len;i++)
    {
        S.ch[i] = s[i];
        S.length ++;
    }
    S.ch[i] = '\0';
}

void Index(Sqstring &S1,Sqstring &S2)
{
    int i = 0,j  = 0;
    while(i < S1.length&&j < S2.length)
    {
        if(S1.ch[i] == S2.ch[j])
        {
            i++;
            j++;
        }
        else
        {
            i = i - j + 1;
            j = 0;
        }
    }
    if(j >= S2.length)
        printf("YES\n");
    else
        printf("NO\n");
}

int main()
{
    char a[200],b[200];
    while(~scanf("%s%s",a,b))
    {
        Sqstring S1,S2;
        initString(S1);
        initString(S2);
        Creat(S1,a);
        Creat(S2,b);
        Index(S1,S2);
    }
    return 0;
}

 

 

KMP做法:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
#define maxsize 1010
int next[100100];
typedef struct
{
    char *ch;
    int length;
    int stringsize;
}Sqstring;

int initString(Sqstring &S)
{
    S.ch = (char *)malloc(maxsize*sizeof(char ));
    if(!S.ch)
        return -1;
    S.length = 0;
    S.stringsize = maxsize;
    return 0;
}

void Creat(Sqstring &S,char s[])
{
    int i;
    int len = strlen(s);
    for(i = 0;i < len;i++)
    {
        S.ch[i] = s[i];
        S.length ++;
    }
    S.ch[i] = '\0';
}


void Get_next(Sqstring &S)
{
    int i = 0,j = -1;
    next[0] = -1;
    while(i < S.length)
    {
        if(j == -1||S.ch[i] == S.ch[j])
        {
            i++;
            j++;
            next[i] = j;
        }
        else
            j = next[j];
    }
}

void Index(Sqstring &S1,Sqstring &S2)
{
    Get_next(S2);
    int i = 0,j  = 0;
    while(i < S1.length&&j < S2.length)
    {
        if(j == -1||S1.ch[i] == S2.ch[j])
        {
            i++;
            j++;
        }
        else
        {
            j = next[j];
        }
    }
    if(j >= S2.length)
        printf("YES\n");
    else
        printf("NO\n");
}

int main()
{
    char a[200],b[200];
    while(~scanf("%s%s",a,b))
    {
        Sqstring S1,S2;
        initString(S1);
        initString(S2);
        Creat(S1,a);
        Creat(S2,b);
        Index(S1,S2);
    }
    return 0;
}

 

    原文作者:KMP算法
    原文地址: https://blog.csdn.net/Annfan123/article/details/52116047
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞