[kuangbin带你飞]专题十六 KMP

第一题,很简单的板子题,只需要把char 改成int类型就好了

#include <cstdio>  
#include <iostream>  
#include <cstring>  
using namespace std;  
  
const int N = 1000005;  
int Next[N];  
int S[N], T[N];  
int slen, tlen;//注意每次一定要计算长度  
  
void getNext()  
{  
    int j, k;  
    j = 0; k = -1; Next[0] = -1;  
    while(j < tlen)  
        if(k == -1 || T[j] == T[k])  
            Next[++j] = ++k;  
        else  
            k = Next[k];  
  
}  
int KMP_Index()  
{  
    int i = 0, j = 0;  
    getNext();  
    while(i < slen && j < tlen)  
    {  
        if(j == -1 || S[i] == T[j])  
        {  
            i++; j++;  
        }  
        else  
            j = Next[j];  
    }  
    if(j == tlen)  
        return i - tlen+1;  
    else  
        return -1;  
}  
int main()  
{  
  
    int TT;  
    int i, cc;  
    scanf("%d",&TT);  
    while(TT--)  
    {
		int i,j,m,n;
		scanf("%d%d",&n,&m);
		for(i=0;i<n;i++)
		{
			scanf("%d",&S[i]); 
		  }  
		for(j=0;j<m;j++) 
		{
			 scanf("%d",&T[j]); 
		}
        slen = n;  
        tlen = m; 
        printf("%d\n",KMP_Index());
    }  
    return 0;  
}  

第二题,仍然为板子题,求母串里有几个子串

#include <cstdio>  
#include <iostream>  
#include <cstring>  
using namespace std;  
  
const int N = 1000005;  
int Next[N];  
char S[N], T[N];  
int slen, tlen;//注意每次一定要计算长度  
  
void getNext()  
{  
    int j, i,k;  
    j = 0; k = -1; Next[0] = -1;  
    while(j < tlen)  
        if(k == -1 || T[j] == T[k])
		{
			Next[++j] = ++k; 
		  }  
        else  
        {
        	 k = Next[k];
		}
}  
int KMP_Count()  
{  
    int ans = 0;  
    int i, j = 0;  
  
    if(slen == 1 && tlen == 1)  
    {  
        if(S[0] == T[0])  
            return 1;  
        else  
            return 0;  
    }  
    getNext();  
    for(i = 0; i < slen; i++)  
    {  
        while(j>=0 && S[i] != T[j])  
            j = Next[j];  
        if(j==-1||S[i] == T[j])  
            j++;  
        if(j == tlen)  
        {  
            ans++;  
            j = Next[j];  
        }  
    }  
    return ans;  
}  
int main()  
{  
  
    int TT;  
    int i, cc;  
    scanf("%d",&TT);  
    while(TT--)  
    {
		scanf("%s",T);
		scanf("%s",S);
        slen = strlen(S);  
        tlen = strlen(T); 
        printf("%d\n",KMP_Count());
    }  
    return 0;  
}  

第三题,仍然板子,但是要注意,这里不能重复使用,所以当j==tlen时,j不再等于Next[j]而是直接等于0;

#include <cstdio>  
#include <iostream>  
#include <cstring>  
using namespace std;  
  
const int N = 1000005;  
int Next[N];  
char S[N], T[N];  
int slen, tlen;//注意每次一定要计算长度  
  
void getNext()  
{  
    int j, i,k;  
    j = 0; k = -1; Next[0] = -1;  
    while(j < tlen)  
        if(k == -1 || T[j] == T[k])
		{
			Next[++j] = ++k; 
		  }  
        else  
        {
        	 k = Next[k];
		}
}  
int KMP_Count()  
{  
    int ans = 0;  
    int i, j = 0;  
  
    if(slen == 1 && tlen == 1)  
    {  
        if(S[0] == T[0])  
            return 1;  
        else  
            return 0;  
    }  
    getNext();  
    for(i = 0; i < slen; i++)  
    {  
        while(j>=0 && S[i] != T[j])  
            j = Next[j];  
        if(j==-1||S[i] == T[j])  
            j++;  
        if(j == tlen)  
        {  
            ans++;  
            j = 0;  
        }  
    }  
    return ans;  
}  
int main()  
{  
  
    int TT;  
    int i, cc;  
    while(scanf("%s",S)!=EOF)
    {
    	if(S[0]=='#')
    	break;
    	scanf("%s",T);
        slen = strlen(S);  
        tlen = strlen(T); 
        printf("%d\n",KMP_Count());
    }  
    return 0;  
}  

第4题,kmp中next数组的应用,主要是找循环节的大小,最少在后面加几个字符才能使其成为循环


#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

const int N = 1000002;  
int Next[N];  
char a[N]; 
int slen, tlen;
void getNext()  
{  
    int j, k;  
    j = 0; k = -1; Next[0] = -1;  
    while(j < tlen)  
        if(k == -1 || a[j] == a[k])  
            Next[++j] = ++k;  
        else  
            k = Next[k];  
}  
int main()
{
	int i,j,m,n;
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%s",a);
		tlen=strlen(a);
		getNext();
		j=tlen-Next[tlen];
		if(tlen%j==0&&j!=tlen)//注意j==tlen的情况,即他本身就是一个循环节 
		printf("0\n");
		else
		{
			printf("%d\n",j-Next[tlen]%j);//注意%j因为像abcabcab中next最后一个为5 
		}
	 } 
	 return 0;
}

### 关于 kuangbin ACM 算法竞赛培训计划 #### 数论基础专题介绍 “kuangbin专题十四涵盖了数论基础知识的学习,旨在帮助参赛者掌握算法竞赛中常用的数论概念和技术。该系列不仅提供了丰富的理论讲解,还推荐了一本详细的书籍《算法竞赛中的初等数论》,这本书包含了ACM、OI以及MO所需的基础到高级的数论知识点[^1]。 #### 并查集应用实例 在另一个具体的例子中,“kuangbin”的第五个专题聚焦于并查集的应用。通过解决实际问题如病毒感染案例分析来加深理解。在这个场景下,给定一组学生及其所属的不同社团关系图,目标是从这些信息出发找出所有可能被传染的学生数目。此过程涉及到了如何高效管理和查询集合成员之间的连通性问题[^2]。 #### 搜索技巧提升指南 对于简单的搜索题目而言,在为期约两周的时间里完成了这一部分内容的学习;尽管看似容易,但对于更复杂的状况比如状态压缩或是路径重建等问题,则建议进一步加强训练以提高解题能力[^3]。 ```python def find_parent(parent, i): if parent[i] == i: return i return find_parent(parent, parent[i]) def union(parent, rank, x, y): rootX = find_parent(parent, x) rootY = find_parent(parent, y) if rootX != rootY: if rank[rootX] < rank[rootY]: parent[rootX] = rootY elif rank[rootX] > rank[rootY]: parent[rootY] = rootX else : parent[rootY] = rootX rank[rootX] += 1 # Example usage of Union-Find algorithm to solve the virus spread problem. ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值