Number Sequence

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9058    Accepted Submission(s): 4149

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
思路:KMP算法,已经略懂
AC代码:
 #include<stdio.h>
int a[], b[];
int fail[];
int n, m, T;
void getfail()
{
fail[] = -;
int i, j;
for(i = , j = -; i < m; i ++)
{
while(j >= && b[j + ] != b[i])
{
j = fail[j];
}
if(b[j + ] == b[i])
j ++;
fail[i] = j;
}
return ;
} int kmp()
{
getfail();
int i, j;
for(i = , j = ; i < n;i ++)
{
while(j && b[j] != a[i])
{
j = fail[j - ] + ;
}
if(b[j] == a[i])
j ++;
if(j == m)
return i - m + ;
}
return -;
} int main(int argc, char const *argv[])
{
scanf("%d", &T);
int i, j;
while(T--)
{
scanf("%d%d", &n, &m);
for(i = ; i < n; i ++)
scanf("%d", &a[i]);
for(i = ; i < m; i ++)
scanf("%d", &b[i]);
getfail();
printf("%d\n", kmp());
}
return ;
}

Number Sequence ----HDOJ 1711的更多相关文章

  1. (KMP 模板)Number Sequence -- Hdu -- 1711

    http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/Other ...

  2. AC日记——Number Sequence hdu 1711

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

  3. Number Sequence HDU 1711(KMP)

    http://acm.hdu.edu.cn/showproblem.php?pid=1711 首次接触KMP,自己都不是特别理解.在网上百度看了好几个帖子之后,对KMP也有了初步的理解. #inclu ...

  4. Number Sequence - HDU 1711(KMP模板题)

    题意:给你一个a串和一个b串,问b串是否是a串的子串,如果是返回b在a中最早出现的位置,否则输出-1   分析:应该是最简单的模板题了吧..... 代码如下: ==================== ...

  5. Number Sequence HDU 1711 KMP 模板

    题目大意:两个数组匹配,求子串首次出现的位置. 题目思路:数组长度,比较大,朴素算法的时间复杂度为 m*n超时.KMP的时间复杂度为m+n可行. #include<iostream> #i ...

  6. Rabin_Karp(hash) HDOJ 1711 Number Sequence

    题目传送门 /* Rabin_Karp:虽说用KMP更好,但是RK算法好理解.简单说一下RK算法的原理:首先把模式串的哈希值算出来, 在文本串里不断更新模式串的长度的哈希值,若相等,则找到了,否则整个 ...

  7. HDOJ 1711 Number Sequence

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

  8. hdoj 1711 Number Sequence【求字串在母串中第一次出现的位置】

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

  9. HDU 1711 Number Sequence(数列)

    HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

随机推荐

  1. iOS9 UITableViewCell separatorInset设置为0分割线还是没有顶到头的问题

    只需要在自定义的Cell中添加以下代码即可 override func awakeFromNib() { super.awakeFromNib() layoutMargins = UIEdgeInse ...

  2. 九度OJ 1124 Digital Roots -- 数位拆解

    题目地址:http://ac.jobdu.com/problem.php?pid=1124 题目描述: The digital root of a positive integer is found ...

  3. #添加屏蔽IP LINUX

    netfilter/iptables 的最大优点是它可以配置有状态的防火墙,这是 ipfwadm 和 ipchains 等以前的工具都无法提供的一种重要功能.有状态的防火墙能够指定并记住为发送或接收信 ...

  4. php安装中的错误

    http://www.cnblogs.com/kristain/articles/3809243.html     借鉴php安装错误 2013-01-04 19:16:49 分类: 系统运维 环境: ...

  5. Shell符号展开

    字符 展开 * 这个 “*” 字符意味着匹配文件名中的任意字符 shell 把 “*” 展开成了另外的东西 ,在 echo 命令被执行前. ~家目录 算术表达式展开 算术表达式展开使用这种格式: $( ...

  6. mongoDB单元测试

    http://blog.csdn.net/kiwi_coder/article/details/37873093 内存mongo https://github.com/flapdoodle-oss/d ...

  7. DelphiXE4- System.IOUtils.TDirectory笔记查询后缀名为dll的文件

    TStringDynArray 在System.Types中定义

  8. 【STM32】STM32 GPIO模式理解

    stm32的GPIO的配置模式有好几种,包括: 1. 模拟输入: 2. 浮空输入: 3. 上拉输入: 4. 下拉输入: 5. 开漏输出: 6. 推挽输出: 7. 复用开漏输出: 8. 复用推挽输出 如 ...

  9. Codeforces Round #321 div2

    好像前几场的题解忘记写了, Orz 状态太差, 平均出两题   都不好意思写了 , 连掉4场, 都要哭晕了. 很水的一场, 写完A B C就去睡了  D题其实不难, E题研究Ing(已用一种奇怪的姿势 ...

  10. bzoj 3065: 带插入区间K小值 替罪羊树 && AC300

    3065: 带插入区间K小值 Time Limit: 60 Sec  Memory Limit: 512 MBSubmit: 1062  Solved: 253[Submit][Status] Des ...