KMP算法

比较好的KMP博客 

KMP是一种改进型匹配字符串的算法 从模板串中找到对应的目标串 包括KMP函数和NEXT函数 下面以俩道水题为例介绍模板

next函数的值是如何判断的呢?这 其中涉及到真前缀 真后缀的问题

 - "A"的前缀和后缀都为空集,共有元素的长度为0;

  - "AB"的前缀为[A],后缀为[B],共有元素的长度为0;

  - "ABC"的前缀为[A, AB],后缀为[BC, C],共有元素的长度0;

  - "ABCD"的前缀为[A, AB, ABC],后缀为[BCD, CD, D],共有元素的长度为0;

  - "ABCDA"的前缀为[A, AB, ABC, ABCD],后缀为[BCDA, CDA, DA, A],共有元素为"A",长度为1;

  - "ABCDAB"的前缀为[A, AB, ABC, ABCD, ABCDA],后缀为[BCDAB, CDAB, DAB, AB, B],共有元素为"AB",长度为2;

  - "ABCDABD"的前缀为[A, AB, ABC, ABCD, ABCDA, ABCDAB],后缀为[BCDABD, CDABD, DABD, ABD, BD, D],共有元素的长度为0。

pmt 0000120

next后移一位第一位加一个-1

        -1000012

 

Description

Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 10000, 1 <= N <= 1000000). Your task is to find a number K which make a[K] = b[1], a[K + 1] = b[2], ...... , a[K + M - 1] = b[M]. If there are more than one K exist, output the smallest one.

 

Input

The first line of input is a number T which indicate the number of cases. Each case contains three lines. The first line is two numbers N and M (1 <= M <= 10000, 1 <= N <= 1000000). The second line contains N integers which indicate a[1], a[2], ...... , a[N]. The third line contains M integers which indicate b[1], b[2], ...... , b[M]. All integers are in the range of [-1000000, 1000000].

 

Output

For each test case, you should output one line which only contain K described above. If no such K exists, output -1 instead.

 

Sample Input

 2
13 5 
1 2 1 2 3 1 2 3 1 3 2 1 2 1 2 3 1 3 
13 5 
1 2 1 2 3 1 2 3 1 3 2 1 2 1 2 3 2 1

 

Sample Output

 

6

-1

 

题意是给定一个文本串一个模板串 一个目标串  匹配模板串 返回值 如果匹配不上返回-1

输入的第一行是数字T,表示情况的数量。每个案例包含三行。第一行是两个数字N和M (1 <= M <= 10000, 1 <= N <= 1000000)。第二行包含N个整数,表示[1],[2],…[N]。第三行包含M个整数,表示b[1], b[2],…,b[M]。所有整数都在[-1000000,1000000]范围内。

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

void makeNext(char b[],int nextn[],int m)
{
    int q,k;
    q=0,k=-1;
    nextn[0]=-1;
    while(q<m)
    {
        if(k==-1||b[q]==b[k])
        {
            q++,k++;
            nextn[q]=k;
        }
        else
            k=nextn[k];
    }
}
int KMP(char b[],char a[],int m, int n, int nextn[])
{
    int d=0;
    int i = 0;
    int j = 0;
    int k;
    while (i<n)
    {
        if (j==-1||a[i]==b[j])
        {
            i++;
            j++;
        }
        else
            j = nextn[j];
        if(j == m)
        {
            d++;
           j = nextn[j];
        }
    }
    return d;
}
char a[1000010],b[10010];
int nextn[10010];
int main()
{
    ios::sync_with_stdio(false);
    int t,n,m,h;
    cin>>t;
    while(t--)
    {
        gets(b);
        gets(a);
        m=strlen(b);
        n=strlen(a);
        makeNext(b,nextn,m);
        h=KMP(b,a,m,n,nextn);
        cout<<h<<endl;
    }
    return 0;
}

一个文本W和一个文本T,计算W在T中出现的次数。

输入文件的第一行包含一个数字:接下来的测试用例的数量。每个测试用例的格式如下:
一行与“W”这个词,一个字符串在{ ' a ',' B ',' C ',…,“Z”},1≤|女|≤10000 W(这里| |表示字符串的长度W)。

一行文本T,一个字符串在{ ' a ',' B ',' C ',…,“Z”},于W | |≤T | |≤1000000。

Input

The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:

  • One line with the word W, a string over {'A', 'B', 'C', …, 'Z'}, with 1 ≤ |W| ≤ 10,000 (here |W| denotes the length of the string W).
  • One line with the text T, a string over {'A', 'B', 'C', …, 'Z'}, with |W| ≤ |T| ≤ 1,000,000.

Output

For every test case in the input file, the output should contain a single number, on a single line: the number of occurrences of the word W in the text T.

Sample Input

3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN

Sample Output

1
3
0
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

void makeNext(char b[],int nextn[],int m)
{
    int q,k;
    q=0,k=-1;
    nextn[0]=-1;
    while(q<m)
    {
        if(k==-1||b[q]==b[k])
        {
            q++,k++;
            nextn[q]=k;
        }
        else
            k=nextn[k];
    }
}
int KMP(char b[],char a[],int m, int n, int nextn[])
{
    int d=0;
    int i = 0;
    int j = 0;
    int k;
    while (i<n)
    {
        if (j==-1||a[i]==b[j])
        {
            i++;
            j++;
        }
        else
            j = nextn[j];
        if(j == m)
        {
            d++;
           j = nextn[j];
        }
    }
    return d;
}
char a[1000010],b[10010];
int nextn[10010];
int main()
{
    //ios::sync_with_stdio(false); 开同步以后cin的回车会被gets读取进去
    int t,n,m,h;
    //scanf("%d",&t);  用cin 或scanf 并不影响
    cin>>t;
    getchar();
    while(t--)
    {
        gets(b);
        gets(a);
        m=strlen(b);
        n=strlen(a);
        makeNext(b,nextn,m);
        h=KMP(b,a,m,n,nextn);
        cout<<h<<endl;
    }
    return 0;
}

 

内容概要:本文档详细介绍了一个基于MATLAB实现的跨尺度注意力机制(CSA)结合Transformer编码器的多变量时间序列预测项目。项目旨在精准捕捉多尺度时间序列特征,提升多变量时间序列的预测性能,降低模型计算复杂度与训练时间,增强模型的解释性可视化能力。通过跨尺度注意力机制,模型可以同时捕获局部细节全局趋势,显著提升预测精度泛化能力。文档还探讨了项目面临的挑战,如多尺度特征融合、多变量复杂依赖关系、计算资源瓶颈等问题,并提出了相应的解决方案。此外,项目模型架构包括跨尺度注意力机制模块、Transformer编码器层输出预测层,文档最后提供了部分MATLAB代码示例。 适合人群:具备一定编程基础,尤其是熟悉MATLAB深度学习的科研人员、工程师研究生。 使用场景及目标:①需要处理多变量、多尺度时间序列数据的研究应用场景,如金融市场分析、气象预测、工业设备监控、交通流量预测等;②希望深入了解跨尺度注意力机制Transformer编码器在时间序列预测中的应用;③希望通过MATLAB实现高效的多变量时间序列预测模型,提升预测精度模型解释性。 其他说明:此项目不仅提供了一种新的技术路径来处理复杂的时间序列数据,还推动了多领域多变量时间序列应用的创新。文档中的代码示例详细的模型描述有助于读者快速理解复现该项目,促进学术技术交流。建议读者在实践中结合自己的数据集进行调试优化,以达到最佳的预测效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值