hdoj 1711 Number Sequence【求字串在母串中第一次出现的位置】
Number Sequence
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15017 Accepted Submission(s):
6585
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.
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].
only contain K described above. If no such K exists, output -1
instead.
#include<stdio.h>
#include<string.h>
#define MAX 10010
int n,m;
int b[MAX],a[MAX*100];
int next[MAX];
void getmap()
{
int i;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<m;i++)
scanf("%d",&b[i]);
}
void getfail()
{
int i,j;
next[0]=next[1]=0;
for(i=1;i<m;i++)
{
j=next[i];
while(j&&b[i]!=b[j])
j=next[j];
next[i+1]=b[i]==b[j]?j+1:0;
}
}
void kmp()
{
int i,j=0;
int ok=0;
for(i=0;i<n;i++)
{
while(j&&a[i]!=b[j])
j=next[j];
if(a[i]==b[j])
j++;
if(j==m)
{
ok=1;
printf("%d\n",i-m+2);//难点在这里
return ;
}
}
if(!ok)
printf("-1\n");
}
int main()
{
int i,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
getmap();
getfail();
kmp();
}
return 0;
}
hdoj 1711 Number Sequence【求字串在母串中第一次出现的位置】的更多相关文章
- Rabin_Karp(hash) HDOJ 1711 Number Sequence
题目传送门 /* Rabin_Karp:虽说用KMP更好,但是RK算法好理解.简单说一下RK算法的原理:首先把模式串的哈希值算出来, 在文本串里不断更新模式串的长度的哈希值,若相等,则找到了,否则整个 ...
- HDOJ 1711 Number Sequence
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 1711 Number Sequence(KMP匹配数字串)
这个就是kmp的数组形式,不用来处理字符串还真有点不习惯呢... #include<iostream> using namespace std; ,MAXM = ; int T[MAXN] ...
- HDU 1711 Number Sequence 【KMP应用 求成功匹配子串的最小下标】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/O ...
- HDU 1711 Number Sequence(数列)
HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- HDU 1711 - Number Sequence - [KMP模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...
- HDU 1711 Number Sequence (字符串匹配,KMP算法)
HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...
- HDU 1711 Number Sequence(KMP)附带KMP的详解
题目代号:HDU 1711 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/ ...
- HDU 1711 Number Sequence (KMP 入门)
Number Sequence Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and ...
随机推荐
- iOS 获取通讯录里边的电话号码AddressBook
1 首先导入库 <AddressBook/AddressBook.h> 2 然后在导入#import <AddressBook/AddressBook.h>文件 3 声明 ...
- 关于C#的编译与执行
每一种编程语言,要想执行,就必须要转换为目标操作系统能够理解的语言才能执行,这种语言叫做本机代码(native code).C#也是一样的,也要做这样的转换,但是它不是一处到位的,在.NET Fram ...
- Using GUID to generate the unique file name in C#
GUID, the abbreviation of "Global Unique Identifier", is a unique reference number used as ...
- IOS DLNA PlatinumKit库的使用
前段时间进行了IOS DLNA的开发,使用的是PlatinumKit库.网上查了很多资料都未果,经过自己的摸索,遂将如何使用PlatinumKit进行DLNA的开发分享给大家. 1.PlatinumK ...
- java dos下中文乱码
代码如下: public class PrintString{ public static void main(String args[]){ System.out.println("\\* ...
- Python直接迭代序列比通过索引迭代序列快。
小脚本跑一下看看时间. 原理:直接迭代序列是通过Python内置的迭代器去实现的,而如果迭代序列需要先造一个可迭代的序列出来.内置的迭代器并不是一下将所有的数据放入内存中,而是需要多少取多少. #!/ ...
- php函数——『解析 xml数据』
<?php //该文件是 //$raw_post_data = file_get_contents('php://input'); //file_put_contents('a.txt', $r ...
- python【第二篇】列表、元组、字典及文件操作
本节内容 列表 元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1.列表 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作:列表有序.可变.元素 ...
- HDU 1069 Monkey and Banana(LIS最长上升子序列)
B - LIS Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descripti ...
- 符合搜索引擎SEO规则的HTML代码
实话说,部落在有时候,也经常会修改一下自己的主题,当然,很多时候,对自己修改过后的主题,会通过查看源代码的方式,来查看自己HTML代码,很多时候,也没有去刻意对代码进行符合搜索引擎SEO规则的优化,而 ...