PTA KMP 串的模式匹配 (25 分)

PTA数据结构 专栏收录该内容
11 篇文章 0 订阅
#include <bits/stdc++.h>
using namespace std;

typedef int Pos;
#define NotFound -1

void BuildMatch(char *pattern, int *match);
Pos KMP(char *str, char *pattern);
int main()
{
	#ifdef ONLINE_JUDGE
	#else
	freopen("in.txt", "r", stdin);
	#endif
	
	char str[1000001] = {0};
	char pattern[100001] = {0};
	scanf("%s\n", (char *)str);//有修改 
	//gets(str);
	int N;
	scanf("%d", &N);
	int i;
	
	for(i = 0; i < N; i++){
		scanf("%s\n", (char *)pattern);//有修改 
		//gets(pattern);
		
		Pos p = KMP(str, pattern);
		if(p != NotFound){
			
			if(i == N - 1){
				printf("%s", str + p);
			}else{
				printf("%s\n", str + p);
			}
			
		}else{
			
			if(i == N -1){
				printf("Not Found");
			}else{
				printf("Not Found\n");
			}
		}
	}
 
 	return 0;
} 
void BuildMatch(char *pattern, int *match)
{
	Pos i, j;
	int m = strlen(pattern);
	match[0] = -1;
	
	for(j = 1; j < m; j++){
		i = match[j - 1];
		
		while((i >= 0) && (pattern[i + 1] != pattern[j])){
			i = match[i];
		}
		if(pattern[i + 1] == pattern[j]){
			match[j] = i + 1;
		}else{
			match[j] = -1;
		}
	}
}

Pos KMP(char *str, char *pattern)
{
	int n = strlen(str);
	int m = strlen(pattern);
	Pos s = 0, p = 0, *match;
	
	if(n < m) return NotFound;
	match = (Pos *)malloc(sizeof(Pos) * m);
	BuildMatch(pattern, match);
	while(s < n && p < m){
		
		if(str[s] == pattern[p]){
			s++;
			p++;
		}else if(p > 0) p = match[p - 1] + 1;
		else s++;
		
	}
	
	return (p == m) ? (s - m) : NotFound;
}
  • 0
    点赞
  • 0
    评论
  • 1
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值