【字符串】tire树 kmp

for (int i = 2, j = 0; i <= m; i ++ )
{
    while (j && p[i] != p[j + 1]) j = ne[j];
    if (p[i] == p[j + 1]) j ++ ;
    ne[i] = j;
}


for (int i = 1, j = 0; i <= n; i ++ )
{
    while (j && s[i] != p[j + 1]) j = ne[j];
    if (s[i] == p[j + 1]) j ++ ;
    if (j == m)
    {
        j = ne[j];
    
    }

Oulipo - POJ 3461 - Virtual Judge (vjudge.net.cn)

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

int ans=0;
int n,m;
char s[1000001];
char p[10001];
int ne[10001];

void kmp(){


for(int i=2,j=0;i<=n;i++){
    while(j&&p[i]!=p[j+1])j=ne[j];

    if(p[i]==p[j+1])j++;

    ne[i]=j;
}

for(int i=1,j=0;i<=m;i++){
while(j&&s[i]!=p[j+1])j=ne[j];

    if(s[i]==p[j+1])j++;

    if(j==n){ans++;
            j=ne[j];}
}

}
int main(){

int t;cin>>t;

while(t--){
        ans=0;

scanf("%s%s",p+1,s+1);
//cin>>p+1>>s+1;
n=m=0;
for(n=1;p[n];n++);
n--;
for(m=1;s[m];m++);
m--;
        //cout<<n<<" "<<m<<" ";
kmp();
cout<<ans<<endl;
}

}

Seek the Name, Seek the Fame - POJ 2752 - Virtual Judge (vjudge.net.cn)

过不了

之后还得看字符串哈希

P6018 [Ynoi2010] Fusion tree - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

P6623 [省选联考 2020 A 卷] 树 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

相关推荐

  1. 字符串tire kmp

    2024-07-13 02:08:04       21 阅读
  2. KMP-重复子字符串

    2024-07-13 02:08:04       58 阅读
  3. Tire 字典、前缀

    2024-07-13 02:08:04       29 阅读
  4. 使用verillog编写KMP字符串匹配算法

    2024-07-13 02:08:04       44 阅读
  5. Day2 字符串哈希&KMP

    2024-07-13 02:08:04       28 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-13 02:08:04       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-13 02:08:04       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-13 02:08:04       58 阅读
  4. Python语言-面向对象

    2024-07-13 02:08:04       69 阅读

热门阅读

  1. 【TVM 教程】使用 TVM 部署框架预量化模型

    2024-07-13 02:08:04       20 阅读
  2. RabbitMq,通过prefetchCount限制消费并发数

    2024-07-13 02:08:04       20 阅读
  3. C#中的泛型

    2024-07-13 02:08:04       18 阅读
  4. 力扣636.函数的独占时间

    2024-07-13 02:08:04       19 阅读
  5. day19打卡

    2024-07-13 02:08:04       16 阅读
  6. 【qml学习笔记】在qml中连接信号与槽

    2024-07-13 02:08:04       22 阅读
  7. 【第20章】MyBatis-Plus逻辑删除支持

    2024-07-13 02:08:04       13 阅读
  8. AI agents 印象

    2024-07-13 02:08:04       18 阅读