码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构实验之串三:KMP应用(KMP模板)

时间:2020-04-11 18:20:01      阅读:60      评论:0      收藏:0      [点我收藏+]

标签:include   结构   https   targe   long   def   get   ret   bsp   

数据结构实验之串三:KMP应用(KMP模板)

技术图片

 

 AC_Code:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <map>
 6 #include <stack>
 7 using namespace std;
 8 typedef long long ll;
 9 int Nex[1000000];
10 char st[1000000];
11 char str[1000000];
12 
13 void Find_Nex()
14 {
15     int i=0,j=-1;
16     Nex[0]=-1;
17     int len=strlen(str);
18     while( i<len )
19     {
20         if( j==-1 || str[i]==str[j] )
21         {
22             i++;
23             j++;
24             Nex[i]=j;
25         }
26         else j=Nex[j];
27     }
28 }
29 
30 void kmp()
31 {
32     int i=0,j=0;
33     int len1=strlen(st);
34     int len2=strlen(str);
35     while( i<len1&&j<len2 )
36     {
37         if( j==-1 || str[j]==st[i])
38         {
39             i++;
40             j++;
41         }
42         else j=Nex[j];
43     }
44     if( j==len2 ) cout<<i-j+1<<endl;
45     else cout<<"-1"<<endl;
46 }
47 
48 
49 int main()
50 {
51     while(~scanf("%s",st))
52     {
53         scanf("%s",str);
54         memset(Nex,0,sizeof(Nex));
55         Find_Nex();
56         kmp();
57     }
58     return 0;
59 }

 

数据结构实验之串三:KMP应用(KMP模板)

标签:include   结构   https   targe   long   def   get   ret   bsp   

原文地址:https://www.cnblogs.com/wsy107316/p/12681261.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!