牛客NC15879 A Simple Problem
传送门:A Simple Problem
题意
题解
代码
1 #include<bits/stdc++.h>
2 #define ull unsigned long long
3 #define ll long long
4 #define pb push_back
5 #define ft first
6 #define sd second
7 #define pii pair<int,int>
8 #define pll pair<ll,ll>
9 using namespace std;
10
11 const int maxn=2e5+10;
12 int s1[maxn],s2[maxn],nt[maxn],pre[maxn],pos2[maxn],pos1[maxn];
13
14 void getpre(int s[],int pos[],int n,int k)
15 {
16 for(int i=0;i<k;i++) pre[i]=-1;
17 for(int i=0;i<n;i++){
18 if(pre[s[i]]==-1) pos[i]=-1;
19 else pos[i]=i-pre[s[i]];
20 pre[s[i]]=i;
21 }
22 }
23
24 void getnt(int s[],int n)
25 {
26 int i=0,j=-1;
27 nt[0]=-1;
28 while(i<n){
29 if(j==-1||(j<s[i]?s[j]==-1:s[i]==s[j])) i++,j++,nt[i]=j;
30 else j=nt[j];
31 }
32 }
33
34 int kmp(int s[],int p[],int n,int m)
35 {
36 getnt(p,m);
37 int i=0,j=0,ans=0;
38 while(i<n){
39 if(j==-1||(j<s[i]?p[j]==-1:s[i]==p[j])) i++,j++;
40 else j=nt[j];
41 if(j==m) ans++;
42 }
43 return ans;
44 }
45
46 int main()
47 {
48 int n,k,m;
49 scanf("%d%d",&n,&k);
50 for(int i=0;i<n;i++) scanf("%d",&s1[i]);
51 scanf("%d",&m);
52 for(int i=0;i<m;i++) scanf("%d",&s2[i]);
53 getpre(s1,pos1,n,k);
54 getpre(s2,pos2,m,k);
55 printf("%d\n",kmp(pos1,pos2,n,m));
56 return 0;
57 }
牛客NC15879 A Simple Problem的更多相关文章
- 【并查集缩点+tarjan无向图求桥】Where are you @牛客练习赛32 D
目录 [并查集缩点+tarjan无向图求桥]Where are you @牛客练习赛32 D PROBLEM SOLUTION CODE [并查集缩点+tarjan无向图求桥]Where are yo ...
- 【牛客网】Whalyzh's Problem
[牛客网]Whalyzh's Problem 每个\(b_{i,j}\)建一个点,认为选了\(b_{i,j}\)一定会选\(a_{i}\)和\(a_{j}\) 选了\(a_{i}\)的话会带了一个\( ...
- 2019牛客暑期多校训练营(第二场)F.Partition problem
链接:https://ac.nowcoder.com/acm/contest/882/F来源:牛客网 Given 2N people, you need to assign each of them ...
- 牛客多校第十场-D- Rikka with Prefix Sum
链接:https://www.nowcoder.com/acm/contest/148/D来源:牛客网 Prefix Sum is a useful trick in data structure p ...
- 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...
- 牛客暑期ACM多校 第七场
链接:https://www.nowcoder.com/acm/contest/145/C来源:牛客网 C .题目描述 A binary string s of length N = 2n is gi ...
- [ACM] poj 3468 A Simple Problem with Integers(段树,为段更新,懒惰的标志)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 55273 ...
- POJ3468 A Simple Problem with Integers 【段树】+【成段更新】
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 57666 ...
- 牛客网第9场多校E(思维求期望)
链接:https://www.nowcoder.com/acm/contest/147/E 来源:牛客网 题目描述 Niuniu likes to play OSU! We simplify the ...
随机推荐
- Centos7 密钥对登陆(适用于群晖DSM)
www.swack.cn - 原文链接:Centos7 密钥对登陆(适用于群晖DSM) 1.生成证书 此处证书使用swack用户生成 注:不要使用root生成证书,因为我们后面会禁用root登陆 [s ...
- PHPstorm 配置主题
1.首先先去下载自己喜欢的主题:http://www.phpstorm-themes.com/ 但是在下载的时候会发现一个问题,在点击下载后,并没有下载,而是会打开这个文件(不同的浏览器不同)但是如果 ...
- collection常用功能:
collection常用功能: Collection是所有单列集合的父接口,因此在collection中定义了单列集合(List)和(Set)通用的一些方法.这些方法可用于操作所有的单列集合,方法如下 ...
- Eslint提示const关键字被保留
如果在使用eslint的时候提示: error Parsing error: The keyword 'const' is reserved 有可能是因为eslint默认审查的es5,需要明确让他审查 ...
- Python基础语法5-控制流语句
- git revert 回退已经push的内容
如题,在日常的开发过程中,可能有组员不小心一下子吧文件修改,需要进行回退 回退主要涉及到2种命令,一种是git reset 一种是 git revert git reset 会修改git log提交历 ...
- IDEA 常用的一些 (就几个) 快捷键
快捷键 说明 Ctrl + P 提示类参数 Ctrl + Q 提示类的属性和方法包名 Ctrl + D 复制一行到下一行 Ctrl + F 查找 Ctrl + R 替换 Ctrl + Z 撤销 Ctr ...
- 创建一个简单MyBatis程序
文章目录 MyBatis基础 MyBatis 简介 创建一个MyBatis程序 1. 创建Java项目 2. 加载MyBatis包 3. 编写POJO类和映射文件 4.创建mybatis-config ...
- Leetcode53. 最大子序列和
问题 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 代码 贪心算法 核心思想就是检查之前 i-1 的元素和,如果小于零就舍弃--对应下面第六行 ...
- Centos 7 网卡配置
网卡配置位置 1 /etc/sysconfig/network-scripts/ifcfg-ethx -----其中x为网卡名称,centos 默认网卡名称为ens33 如何配置 1 2 3 4 ...