博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 3461 Oulipo (KMP)
阅读量:7218 次
发布时间:2019-06-29

本文共 852 字,大约阅读时间需要 2 分钟。

基础KMP, 要注意一次查找完成后,到下一可查找处继续匹配,这样才能保证得到最终个数。 

code:

#include<cstdio>
#include<cstring>
char substr[
10001] ;
char str[
1000001] ;
int next[
10001] ;
int sublen, len, ans ;
void get_next(){
    next[
1] = 
0 ;
    
int j = 
0 ;
    
for(
int i=
2; i<=sublen; i++){
        
while(j>
0&&substr[j+
1]!=substr[i])
            j = next[j] ;
        
if(substr[j+
1]==substr[i])  j ++ ;
        next[i] = j ;
    }
}
void kmp(){
    
int j = 
0 ;
    
for(
int i=
1; i<=len; i++){
        
while(j>
0&&substr[j+
1]!=str[i])
            j = next[j] ;
        
if(substr[j+
1]==str[i]) j ++ ;
        
if(j==sublen){
            ans ++ ;
            j = next[j] ;   
//
一次查找完成后,到下一可查找处继续匹配
        }
    }
    printf(
"
%d\n
", ans) ;
}
int main(){
    
int t ;
    scanf(
"
%d
", &t) ;
    
while(t--){
        scanf(
"
%s
", substr+
1) ;
        scanf(
"
%s
", str+
1) ;
        ans = 
0 ;
        sublen = strlen(substr+
1) ;
        len = strlen(str+
1) ;
        get_next() ;
        kmp() ;
    }
    
return 
0 ;

转载于:https://www.cnblogs.com/xiaolongchase/archive/2012/02/05/2339128.html

你可能感兴趣的文章
由1433端口入侵,浅谈sqlserver安全 (转)
查看>>
2个YUV视频拼接技术
查看>>
spring data实现自定义的repository实现类,实现跟jpa联通
查看>>
“惊群”,看看nginx是怎么解决它的
查看>>
Theano3.3-练习之逻辑回归
查看>>
利用RGB-D数据进行人体检测 带dataset
查看>>
live555的编译及使用
查看>>
C++builder XE 安装控件 及输出路径
查看>>
优点和阵列的缺点,并且一个链表
查看>>
CSS3透明属性opacity
查看>>
Genymotion模拟器的安装及常见问题解决方法
查看>>
PHP 乘法口诀表
查看>>
如何彻底关闭windows update
查看>>
SpringMVC+SwfUpload进行多文件同时上传
查看>>
ASP.NET Core中的依赖注入(2):依赖注入(DI)
查看>>
Java_JAVA6动态编译的问题
查看>>
scala 日期格式转换
查看>>
Filtering Specific Columns with cut
查看>>
多线程编程1-NSThread
查看>>
反馈组态的判别
查看>>