SDUT OJ 数据结构实验之串三:KMP应用
数据结构实验之串三:KMP应用
Time Limit: 1000 ms Memory Limit: 65536 KiB
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应用的更多相关文章
- SDUT 3311 数据结构实验之串三:KMP应用
数据结构实验之串三:KMP应用 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 有n个小朋友 ...
- SDUT OJ 数据结构实验之串一:KMP简单应用 && 浅谈对看毛片算法的理解
数据结构实验之串一:KMP简单应用 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...
- SDUT OJ 数据结构实验之串二:字符串匹配
数据结构实验之串二:字符串匹配 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
- SDUT OJ 数据结构实验之二叉树三:统计叶子数
数据结构实验之二叉树三:统计叶子数 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...
- SDUT OJ 数据结构实验之排序三:bucket sort
数据结构实验之排序三:bucket sort Time Limit: 250 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem D ...
- SDUT OJ 数据结构实验之链表三:链表的逆置
数据结构实验之链表三:链表的逆置 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...
- SDUT-3331_数据结构实验之串三:KMP应用
数据结构实验之串三:KMP应用 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 有n个小朋友,每个小朋友手里有一些糖块, ...
- SDUT 2772 数据结构实验之串一:KMP简单应用
数据结构实验之串一:KMP简单应用 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 给定两个 ...
- SDUT 3347 数据结构实验之数组三:快速转置
数据结构实验之数组三:快速转置 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 转置运算是一 ...
随机推荐
- collections、time和datetime模块
主要内容: 一.collections模块 二.time模块 三.datetime模块 1️⃣ collection模块 1.什么是collections模块.干什么用? collections模块 ...
- UIWidget
[UIWidget] UIWidget在NGUI中的层次如下. 根据上篇所述,UIRect实现实现了Anchor功能.而Widget提供的功能也很简单,如下: 可以看到,widget只提供四个属性,a ...
- Leetcode:Divide Two Integers分析和实现
题目要求我们用一个32位整数整除另外一个整数,但是不允许我们使用除法,乘法和取模运算. 有趣的问题,下面说一下我的思路: 首先,先给出两个正整数除法运算的过程.假设a为被除数,而b为除数.在计算机中无 ...
- Docker02 基本命令、开发环境搭建、docker安装nginx、Dockerfile、路径挂载
1 基本命令 1.1 docker相关 centos6.5 安装docker环境 >sudo yum install -y http://mirrors.yun-idc.com/epel/6/i ...
- Docker学习笔记_安装和使用Apache
一.准备 1.宿主机OS:Win10 64位 2.虚拟机OS:Ubuntu18.04 3.账号:docker 二.安装 1.搜索镜像 ...
- c语言学习笔记 break语句
比如 for() { for() { break; } } 那个break语句只是跳出它所在的那个for循环,不会把最外面的for循环都跳出去.
- NUMA微架构
NUMA微架构 written by qingran September 8th, 2011 no comment 现在开始补日志,逐步的扫清以前写了一半的和"欠账未还的".半年之 ...
- Python3 使用requests库读取本地保存的cookie文件实现免登录访问
1. 读取selenium模块保存的本地cookie文件来访问知乎 读取http://www.cnblogs.com/strivepy/p/9233389.html保存的本地cookie来访问知乎的 ...
- jquery select 左右移动
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...
- ESP8266-iot-简介1
ESP8266简介