Number Sequence

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

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匹配

#include <iostream>
#include <cstdio> using namespace std; const int maxn = 1e6+; int a[maxn];
int b[maxn];
int Next[maxn]; void getNext(int len) {
int i = , j = -;
Next[] = -;
while(i < len) {
while(j != - && b[i] != b[j]) {
j = Next[j];
}
Next[++i] = ++j;
}
} int main() {
int T;
scanf("%d", &T);
while(T--) {
int n, m;
scanf("%d %d", &n, &m);
for(int i = ; i < n; i++) {
scanf("%d", &a[i]);
}
for(int j = ; j < m; j++) {
scanf("%d", &b[j]);
}
getNext(m);
int i = , j = ;
int ans = -;
while(i < n) {
while(j != - && a[i] != b[j]) {
j = Next[j];
}
i++, j++;
if(j >= m) {
ans = i - j + ;
break;
}
}
printf("%d\n" ,ans);
}
return ;
}

HDU 1711:Number Sequence(KMP)的更多相关文章

  1. HDU 1711:Number Sequence(KMP模板,求位置)

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

  2. HDU - 1711 A - Number Sequence(kmp

    HDU - 1711 A - Number Sequence   Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1 ...

  3. HDU 1711 Number Sequence(KMP)附带KMP的详解

    题目代号:HDU 1711 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/ ...

  4. HDU 1711 Number Sequence(kmp)

    Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], .... ...

  5. 1711 Number Sequence(kmp)

    Number Sequence Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) To ...

  6. Number Sequence(kmp)

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

  7. HDU 1711 Number Sequence (KMP)

    白书说这个是MP,没有对f 数组优化过,所以说KMP有点不准确 #include <stdio.h> int a,b; int T[1000010],P[10010];//从0开始存 in ...

  8. HDU 2062:Subset sequence(思维)

    Subset sequence Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

  9. HDU 4763:Theme Section(KMP)

    http://acm.hdu.edu.cn/showproblem.php?pid=4763 Theme Section Problem Description   It's time for mus ...

随机推荐

  1. Eclipse 设置新建文件默认编码为 utf-8 的方法

    选择编辑器顶部 Windows->Preferences->搜索jsp->选择utf-8编码->保存.

  2. arcgisJs之featureLayer中feature的获取

    arcgisJs之featureLayer中feature的获取 在featureLayer中source可以获取到一个Graphic数组,但是这个数组属于原数据数组.当使用 applyEdits修改 ...

  3. ubuntu下安装vue/cli提示No command 'vue' found

    通过官方指令 npm install -g @vue/cli 安装vue脚手架提示: No command 'vue' found, did you mean: Command 'vpe' from ...

  4. 小程序中使用components方法selectComponent遇到的坑 返回为null

    前言:哎呦气死了,小程序等着发布审核得时候 发现了一个bug,selectComponent获取不到组件了,返回值一直为null 原因居然是因为 wx:if  , 代码如下,无论if是true还是fa ...

  5. 第三章·MySQL版本区别及管理

    一.MySQL5.6与MySQL5.7安装的区别 1.cmake的时候加入了bostorg 2.初始化时 使用mysqld --initialize 替代mysql_install_db,其它参数没有 ...

  6. 在 Sublime Text 3 中使用 SublimeClang 插件

    SublimeClang 是 Sublime Text 中唯一的 C/C++ 自动补全插件,功能强大,自带语法检查功能,可惜目前作者停止更新了,而且只支持 Sublime Text 2.今晚在 ST ...

  7. 制作CentOS8安装U盘时遇到的“Minimal BASH-like...”问题

    ---恢复内容开始--- CentOS8已经推出了,正好最近新到了块服务器硬盘需要安装系统,就拿过来尝一下鲜. 下载好iso文件后,以制作CentOS7安装盘相同的步骤,用UltroISO(软碟通)往 ...

  8. shell i/o交互及重定向

    标准输入:/dev/stdin,文件描述号:0,默认设备:键盘 标准输出:/dev/stdout,文件描述号:1,默认设备:显示器 标准错误输出:/dev/stderr,文件描述号:2,默认设备:显示 ...

  9. 使用ViewPager实现导航

    1.使用PageAdapter适配器 2.使用FragmentPageAdapter适配器 监听事件: OnPageChangeListener 百度云:链接: http://pan.baidu.co ...

  10. QT对话框

    QFileDialog:文件对话框 QString fileName=QFileDialog::getOpenFileName(this,"打开文件", "/" ...