Number Sequence(HDU1711)
2016-04-15 01:35
483 查看
[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher
Time Limit:5000MS
Memory Limit:32768KB
HDU 1711
Description
Given two sequences of numbers : a[1], a[2], …… , a
, 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
. 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
Sample Output
(我学了好久kmp才明白这个算法) 我是看了俞勇的《ACM国际大学生程序设计竞赛知识与入门》p161懂了匹配的方式
看了http://www.cnblogs.com/c-cloud/p/3224788.html才明白next数组怎么构造
尤其是这两张图特别容易让人明白next数组构造方式
![](http://images.cnitblog.com/blog/432480/201307/30163843-2fd01a5b306b4fbb8183b0a7c145d79c.png)
![](http://images.cnitblog.com/blog/432480/201307/30171002-e67282f4d1d84cb59e0152826b58e6ac.png)
kmp模板:
法二 用hash字符串处理,匹配主串和模板串
看的是大白书(《挑战程序设计竞赛》)p374
只要不断这样计算开始位置右移一位后的字符串子串的哈希值,就可以在O(n)时间内得到所有位置对应的hash值,从而在O(n+m)时间内完成字符串匹配。在实现时,可以用64位无符号整数计算hash值,并取h为2^64,通过自然溢出省去求模运算。
hash模板:
![](https://img-blog.csdn.net/20160415021028618)
![](https://img-blog.csdn.net/20160415021047790)
题目:
A - Number SequenceTime Limit:5000MS
Memory Limit:32768KB
HDU 1711
Description
Given two sequences of numbers : a[1], a[2], …… , a
, 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
. 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
题目大意:
两个数组a,b,a作为主串,b为模板串,寻找a中包含b的最小子串下标分析:
法一 a作为主串,b作为模板串,用kmp匹配(我学了好久kmp才明白这个算法) 我是看了俞勇的《ACM国际大学生程序设计竞赛知识与入门》p161懂了匹配的方式
看了http://www.cnblogs.com/c-cloud/p/3224788.html才明白next数组怎么构造
尤其是这两张图特别容易让人明白next数组构造方式
![](http://images.cnitblog.com/blog/432480/201307/30163843-2fd01a5b306b4fbb8183b0a7c145d79c.png)
![](http://images.cnitblog.com/blog/432480/201307/30171002-e67282f4d1d84cb59e0152826b58e6ac.png)
kmp模板:
//p[]为模板串,n为模板串的长度,现构造next数组 void makenext(int n) { next[0] = 0; for (int i = 1, j = 0; i < n; i++) { while (j>0 && p[j] != p[i]) j = next[j - 1]; if (p[i] == p[j]) { j++; } next[i] = j; } } //T[]为主串,m为主串的长度,p[]为模板串,n为模板串的长度 int kmp(int m, int n) { for (int i = 0, j = 0; i < m; i++) { while (j>0 && T[i] != p[j]) j = next[j - 1]; if (T[i] == p[j]) { j++; } if (j == n)return i - n + 1; } return -1;//主串并不包含模板串 }
法二 用hash字符串处理,匹配主串和模板串
看的是大白书(《挑战程序设计竞赛》)p374
只要不断这样计算开始位置右移一位后的字符串子串的哈希值,就可以在O(n)时间内得到所有位置对应的hash值,从而在O(n+m)时间内完成字符串匹配。在实现时,可以用64位无符号整数计算hash值,并取h为2^64,通过自然溢出省去求模运算。
hash模板:
//al为主串长度,bl为模板串长度 typedef unsigned long long ull; const ull B=100000007;//hash基数 int contain(int al, int bl) { if (bl > al)return -1; int cnt = 0; //计算B的bl次方 ull t = 1; for (int i = 0; i < bl; i++) t *= B; //计算a和b长度为bl的前缀对应的hash值 ull ah = 0, bh = 0; for (int i = 0; i < bl; i++)ah = ah*B + a[i]; for (int i = 0; i < bl; i++)bh = bh*B + b[i]; //对a不断右移一位,更新hash值并判断 for (int i = 0; i+bl <=al; i++) { if (ah == bh)return i; if(i+bl<al) ah = ah*B - a[i] * t + a[i + bl]; } return -1; }
用kmpAC代码:
#include<iostream> #include<algorithm> #include<vector> #include<string> #include<string.h> using namespace std; #define maxnA 1000001 #define maxnB 10001 int a[maxnA]; int b[maxnB]; int nextb[maxnB]; void makenext(int bl) { nextb[0] = 0; for (int i = 1, j = 0; i < bl; i++) { while (j>0 && b[i] != b[j]) j = nextb[j - 1]; if (b[i] == b[j]) { j++; } nextb[i] = j; } } int kmp(int al,int bl) { makenext(bl); for (int i = 0, j = 0; i < al; i++) { while (j > 0 && b[j] != a[i]) j = nextb[j - 1]; if (b[j] == a[i]) j++; if (j == bl) return i - bl + 1; } return -2; } int main() { int cas; scanf("%d", &cas); while (cas--) { memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); memset(nextb, 0, sizeof(nextb)); int al, bl; scanf("%d%d", &al, &bl); for (int i = 0; i < al; i++) scanf("%d", &a[i]); for (int i = 0; i < bl; i++) scanf("%d", &b[i]); int res = kmp(al, bl)+1; printf("%d\n", res); } }
用hash字符串AC代码:
#include<iostream> #include<string> #include<string.h> #include<algorithm> using namespace std; typedef unsigned long long ull; const ull B = 100000007; const int MAXN = 1000001; const int MAXM = 10001; int a[MAXN]; int b[MAXM]; //b是否在a中 int contain(int al, int bl) { if (bl > al)return -2; int cnt = 0; //计算B的bl次方 ull t = 1; for (int i = 0; i < bl; i++) t *= B; //计算a和b长度为bl的前缀对应的hash值 ull ah = 0, bh = 0; for (int i = 0; i < bl; i++)ah = ah*B + a[i]; for (int i = 0; i < bl; i++)bh = bh*B + b[i]; //对a不断右移一位,更新hash值并判断 for (int i = 0; i < al; i++) { if (ah == bh)return i; else ah = ah*B - a[i] * t + a[i + bl]; } return -2; } int main() { int cas; scanf("%d", &cas); while (cas--) { memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); int al, bl; scanf("%d%d", &al, &bl); for (int i = 0; i < al; i++) scanf("%d", &a[i]); for (int i = 0; i < bl; i++) scanf("%d", &b[i]); int res = contain(al, bl)+1; printf("%d\n", res); } }
小结:
个人觉得大多数kmp匹配问题似乎都可以用hash处理,而且hash似乎好写一些(较易理解),况且hash比kmp省了300ms。相关文章推荐
- c语言实现hashmap(转载)
- 数据库链接字符串查询网站
- Flex字符串比较 还有Flex字符串操作
- Ruby中创建字符串的一些技巧小结
- ASP下经常用的字符串等函数参考资料
- 将字符串小写转大写并延时输出的批处理代码
- 将字符串转换成System.Drawing.Color类型的方法
- Lua源码中字符串类型的实现
- Lua性能优化技巧(四):关于字符串
- 字符串聚合函数(去除重复值)
- Ruby中Hash的11个问题解答
- Ruby中的字符串编写示例
- 总结的5个C#字符串操作方法分享
- sqlserver中求字符串中汉字的个数的sql语句
- sql server字符串非空判断实现方法
- VBS的字符串及日期操作相关函数
- C#实现将千分位字符串转换成数字的方法
- jquery 删除字符串最后一个字符的方法解析
- PowerShell实现在字符串中查找大写字母
- PowerShell中使用Out-String命令把对象转换成字符串输出的例子