博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
KMP模板题 Number Sequence HDU1711
阅读量:4550 次
发布时间:2019-06-08

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

模板...嗯

1 #include 
2 #include
3 #include
4 #pragma warning ( disable : 4996 ) 5 using namespace std; 6 7 const int inf = 0x3f3f3f3f; 8 const int maxn = 1e4+5; 9 const int vspot = 1e6+5;10 11 int net[maxn];12 int num[maxn], test[vspot];13 int N, M, ans;14 15 void getnext()16 {17 memset( net, 0, sizeof(net) );18 ans = 0;19 int len = M;20 21 int k = -1, j = 0;22 net[j] = -1;23 24 while ( j < len-1 )25 {26 if ( k==-1 || num[j]==num[k] )27 {28 k++;29 j++;30 net[j] = k;31 }32 else33 k = net[k];34 }35 }36 37 void solve()38 {39 int i = 0, j = 0;40 while ( i < N && j < M )41 {42 if( j == -1 || test[i] == num[j] )43 { i++; j++; }44 else45 j = net[j];46 }47 if( j == M )48 ans = i-j+1;49 else50 ans = -1;51 }52 53 int main()54 {55 int all;56 cin >> all;57 while (all--)58 {59 cin >> N >> M;60 for( int i = 0; i < N; i++ )61 scanf( "%d", &test[i] ); 62 for( int i = 0; i < M; i++ )63 scanf( "%d", &num[i] ); 64 65 getnext();66 solve();67 printf( "%d\n", ans );68 }69 return 0;70 }
View Code

 

转载于:https://www.cnblogs.com/chaoswr/p/8617442.html

你可能感兴趣的文章
禁止键盘上的刷新键F5等
查看>>
SAP中对于获取订单的状态
查看>>
oracle PL/SQL块
查看>>
CentOS7集群环境Elastic配置
查看>>
【EXCEL】指定の項目の内容一覧を表示
查看>>
Head first java chapter 4 对象的行为
查看>>
luogu_4503【题解】企鹅QQ 哈希
查看>>
linux 面试
查看>>
Linux:Gentoo系统的安装笔记(三)
查看>>
打开IE窗口并最大化显示
查看>>
Windows API SendMessage 和 PostMessage 内部实现
查看>>
服务器发送邮件出现Could not connect to SMTP host错误 解决办法
查看>>
sklearn.preprocessing.LabelBinarizer
查看>>
C teaching
查看>>
分隔指定内容,提取章节数
查看>>
this point
查看>>
leetcode 30 Substring with Concatenation of All Words
查看>>
验证登录信息是否合法
查看>>
线程池
查看>>
git版本控制器的基本使用
查看>>