zoukankan      html  css  js  c++  java
  • 洛谷P3375KMP字符串匹配

    传送门

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #define re register
    using namespace std;
    const int maxn = 1000005;
    
    inline long long read() {
    	char ch = getchar();
    	long long f = 1 , x = 0;
    	while(ch > '9' || ch < '0') {if(ch == '-') f = -1 ;ch = getchar();}
    	while(ch >= '0' && ch <= '9') {x = (x << 1) + ( x << 3) + ch - '0';ch = getchar();}
    	return x * f;
    }
    
    char s1[maxn],s2[maxn];
    int len1,len2;
    int nxt[maxn];
    //用于记录当匹配到模式串的第 i 位之后失配,该跳转到模式串的哪个位置
    
    int main(){
    	cin >> s1 + 1 >> s2 + 1 ;
    	len1 = strlen(s1 + 1) , len2 = strlen(s2 + 1);
    	nxt[0] = nxt[1] = 0; 
    	int j ;//j可以看做表示当前已经匹配完的模式串的最后一位的位置 
    	for(re int i = 2 ; i <= len2 ; ++i) {
    		while(j && s2[i] != s2[j + 1])  j = nxt[j];
    		//如果失配 ,那么就不断向回跳,直到可以继续匹配 
    		if(s2[j + 1] == s2[i])  j++;
    		//如果匹配成功,那么对应的模式串位置++ 
    		nxt[i] = j ;
    	}
    	j = 0;
    	for(re int i = 1 ; i <= len1 ; ++i) {
    		while(j && s2[j + 1] != s1[i]) j = nxt[j];
    		//此处判断j是否为0的原因在于,如果回跳到第一个字符就不 用再回跳
    		if(s2[j + 1] == s1[i])  j++ ;
    		if(j == len2) {
    			printf("%d
    ",i - len2 + 1) ;
    			j = nxt[j];
    		}
    	}
    	for(re int i = 1 ; i <= len2 ; ++i)
    		printf("%d " , nxt[i]);
    	return 0;
    }
    
  • 相关阅读:
    Spring MVC中的(多)文件上传和下载
    SSM整合案例
    事务的四种隔离级别和七种传播行为
    注解方式实现IOC和AOP
    顾问包装通知
    使用ProxyFactoryBean进行AOP
    动态代理(jdk&cglib)的用法
    英语中12个典型的中国式错误
    翻译:你的声音太小了,可以大一些吗
    今天天气怎么样
  • 原文地址:https://www.cnblogs.com/Stephen-F/p/9933183.html
Copyright © 2011-2022 走看看