博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU-1711-Number Sequence KMP
阅读量:5999 次
发布时间:2019-06-20

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

Number Sequence

                                                                                                      Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

                                                                                                                                 Total Submission(s): 3036    Accepted Submission(s): 1356

Problem 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
   题目只是将字符串匹配变成了整型数据的匹配,其思想还是一样的,前面没有看见每个数据的范围是-1000000 - 1000000,直接用gets(  )进行读取,后果可想而知,RE了,当然就是数组开的不够大罗。当然如果系统能够让我开个2000000000000的数组应该也行吧,哦,那可能又会超时。
  代码如下:
#include 
#include
int o[1000010], s[10010];int M, N;void getnext( int *s, int *next ){ int k= 1, j= 0; while( k< M ) { if( j== 0|| s[k]== s[j] ) { ++j, ++k; if( s[k]== s[j] ) { next[k]= next[j]; } else { next[k]= j; } } else { j= next[j]; } }}int kmp( int *o, int *s, int *next ){ int k= 0, j= 0; while( k<= N&& j<= M ) { if( j== 0|| o[k]== s[j] ) { ++j, ++k; } else { j= next[j]; } } if( j> M ) { return k- M; } else { return -1; }}int main( ){ int T, next[20010]; scanf( "%d" ,&T ); while( T-- ) { scanf( "%d %d", &N, &M ); for( int i= 1; i<= N; ++i ) { scanf( "%d", &o[i] ); } for( int i= 1; i<= M; ++i ) { scanf( "%d", &s[i] ); } getnext( s, next ); int ans= kmp( o, s, next ); if( ans ) { printf( "%d\n", kmp( o, s, next ) ); } } return 0;}

转载地址:http://kxwmx.baihongyu.com/

你可能感兴趣的文章
去除a标签链接触摸时产生边框
查看>>
[转载]Huffman编码压缩算法
查看>>
webkit支持跨域的方法
查看>>
如何测试php是否连接mysql成功
查看>>
【前缀思想】二叉树中所有距离为 K 的结点
查看>>
基于Abp的WebApi容器
查看>>
【leetcode】991. Broken Calculator
查看>>
SQL Server登录 18456错误
查看>>
flask小例
查看>>
[报到] 发布个自己修改的联动下拉菜单 (无限级、数据库、初始值.)
查看>>
CentOS下yum命令详解
查看>>
Tengine——安装起来真费劲
查看>>
音视频系列之iOS: 音频采集 AudioUnit
查看>>
MYSQL三大范式
查看>>
CSS小技巧--input checkbox css 样式美化
查看>>
89. render: h => h(App) 具体含义解释
查看>>
Storyboard使用心得
查看>>
1996: [Hnoi2010]chorus 合唱队
查看>>
C IO programming test code
查看>>
linux内核中GNU C和标准C的区别
查看>>