数据结构实验之串三:KMP应用

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic Discuss

Problem Description

有n个小朋友,每个小朋友手里有一些糖块,现在这些小朋友排成一排,编号是由1到n。现在给出m个数,能不能唯一的确定一对值l和r(l <= r),使得这m个数刚好是第l个小朋友到第r个小朋友手里的糖块数?

Input

首先输入一个整数n,代表有n个小朋友。下一行输入n个数,分别代表每个小朋友手里糖的数量。

之后再输入一个整数m,代表下面有m个数。下一行输入这m个数。

Output

如果能唯一的确定一对l,r的值,那么输出这两个值,否则输出-1

Sample Input

5
1 2 3 4 5
3
2 3 4

Sample Output

2 4
#include <stdio.h>
#include <stdlib.h>
#include <string.h> int s1[1000005],s2[1000005];
int next[1000005];
void get_next(int *s, int N)
{
next[0] = -1;
int k = -1;
for (int i=1; i<=N-1; i++)
{
while (k>-1 && s[k+1] != s[i])
k = next[k];
if (s[k+1] == s[i])
k++;
next[i] = k;
}
} void KMP( int *s, int *p, int n, int m )
{
int l, r, i;
int cnt = 0;
int k = -1;
for ( i = 0; i < n; i++)
{
while (k >-1&& p[k + 1] != s[i])
k = next[k];
if (p[k + 1] == s[i])
k++;
if (k == m-1)
{
l = i - m + 2;
r = i + 1;
cnt++;
}
}
if( cnt == 1 )
printf("%d %d\n", l, r );
else printf("-1\n");
return ;
} int main()
{
int n, m, i;
scanf("%d", &n);
for(i=0; i<n; i++)
scanf("%d", &s1[i]);
scanf("%d", &m);
for(i=0; i<m; i++)
scanf("%d", &s2[i]);
get_next(s2, m);
KMP(s1, s2, n, m);
return 0;
}

SDUT OJ 数据结构实验之串三:KMP应用的更多相关文章

  1. SDUT 3311 数据结构实验之串三:KMP应用

    数据结构实验之串三:KMP应用 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 有n个小朋友 ...

  2. SDUT OJ 数据结构实验之串一:KMP简单应用 && 浅谈对看毛片算法的理解

    数据结构实验之串一:KMP简单应用 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...

  3. SDUT OJ 数据结构实验之串二:字符串匹配

    数据结构实验之串二:字符串匹配 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...

  4. SDUT OJ 数据结构实验之二叉树三:统计叶子数

    数据结构实验之二叉树三:统计叶子数 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...

  5. SDUT OJ 数据结构实验之排序三:bucket sort

    数据结构实验之排序三:bucket sort Time Limit: 250 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem D ...

  6. SDUT OJ 数据结构实验之链表三:链表的逆置

    数据结构实验之链表三:链表的逆置 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...

  7. SDUT-3331_数据结构实验之串三:KMP应用

    数据结构实验之串三:KMP应用 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 有n个小朋友,每个小朋友手里有一些糖块, ...

  8. SDUT 2772 数据结构实验之串一:KMP简单应用

    数据结构实验之串一:KMP简单应用 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 给定两个 ...

  9. SDUT 3347 数据结构实验之数组三:快速转置

    数据结构实验之数组三:快速转置 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 转置运算是一 ...

随机推荐

  1. JBPM具体应用之decision节点的使用

    JBPM工作流引擎为我们提供了许多的节点应用,每一个节点都有其不同的作用,其中有四个比较常用的节点,他们分别decision,fork,state和task.在本文中我们先介绍decision节点,余 ...

  2. 仿函数(二、stl中常用仿函数)

    提到C++ STL,首先被人想到的是它的三大组件:Containers, Iterators, Algorithms,即容器,迭代器和算法.容器为用户提供了常用的数据结构,算法大多是独立于容器的常用的 ...

  3. PHP json_encode 让URL//不转义

    $json_info=json_encode((object)$data,JSON_UNESCAPED_SLASHES);

  4. EF删除,查询,Linq查询,Lambda查询,修改链接字符串

    (1)//删除操作 public bool delete() { try { a_context = new AEntities(); b1 = new Table_1(); //删除只需要写主键就行 ...

  5. 409. Longest Palindrome 最长对称串

    [抄题]: Given a string which consists of lowercase or uppercase letters, find the length of the longes ...

  6. 用TCGA数据库分析癌症和癌旁组织的表达差异

    上周收到一条求助信息:“如何用TCGA数据库分析LINC00152在卵巢癌与正常组织的的表达差异?” 所以以这个题目为记录分析过程如下: 一.下载数据 a)进入网站https://cancergeno ...

  7. 10.Execution failed with exit status: 3

    错误信息: insert overwrite table t_mobile_mid_use_p_tmp4_rcf select '201411' as month_id, a.prov_id, a.c ...

  8. 学习Vue.js需要了解的部分内容

    重要: 1.如果要通过js/模板引用 图片到项目,图片路径需要使用require. 2.$event: $event 等于$emit 抛出的值,还可以使用$event.target.value. $e ...

  9. Django开发——集成的子框架django.contrib

    Django开发——集成的子框架django.contrib 2018年09月11日 19:32:42 Mrkang1314 阅读数:63  https://blog.csdn.net/mashaok ...

  10. [redis]redis-cluster搭建

    1.概述: redis是一种工作在内存里no-sql的非关系型数据库,广泛应用于缓存需求,以减少大量的数据访问对数据库的压力,还很适合用来充当整个互联网架构中各级之间的cache 比如lvs的4层转发 ...