KMP字符串模式匹配算法实现

网址:http://zju.acmclub.com/index.php?app=problem_title&id=1&problem_id=1751

简单题

#include<stdio.h>
#include<string.h>
char str[100],temp[100];
int main()
{
    int c = 3;
    while(c--)
    {
    scanf("%s%s",str,temp);
    int ls = strlen(str), lt = strlen(temp);
    int i,j,sum = 0;
    for(i = 0; i < ls; i++)
    {
        sum = 0;
        if(str[i] == temp[0])
        {
            int m;
            for(j = 0, m = i; j < lt; j++ ,m++)
            {
                if(str[m] == temp[j])
                    sum++;
            }
            if(sum == lt)
            {
                printf("%d\n",i + 1);
                break;
            }
            sum = 0;
        }
    }
    if(sum == 0)
        printf("0\n");
    }  
    return 0;
}


你可能感兴趣的:(数据结构)