牛客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 ...
随机推荐
- Server 2012 R2 Standard 安装运行PCS7时出现“无法启动此程序,因为计算机中丢失api-ms-win-crt-runtime-l1-1-0.dll”解决方法
网上看到了这篇文章https://www.jianshu.com/p/21f4bb8b5502,根据思路自己尝试,解决了丢失的问题.提示[计算机中丢失api-ms-win-crt-runtime-l1 ...
- 【Azure Developer】Python代码通过AAD认证访问微软Azure密钥保管库(Azure Key Vault)中机密信息(Secret)
关键字说明 什么是 Azure Active Directory?Azure Active Directory(Azure AD, AAD) 是 Microsoft 的基于云的标识和访问管理服务,可帮 ...
- 十八:SQL注入之堆叠及绕WAF
堆叠查询注入 (双查询注入) stacked injections(堆叠注入)从名词的含义就可以看到是一堆的SQL语句一起执行,而在真实的运用中也是这样的,我们知道在mysql中,主要是命令行中,每一 ...
- 【Git】4、创建代码仓库,HTTP、SSH拉取远端代码
拉取远端代码:使用Git命令下载远程仓库到本地 文章目录 拉取远端代码:使用Git命令下载远程仓库到本地 1.创建远程代码仓库 2.创建仓库 3.进入仓库 4.HTTP(S)获取远程仓库 首次拉取 更 ...
- 使用yaml配置文件管理资源
[root@k8s-master ~]# vim nginx-deployment.yaml apiVersion: apps/v1beta2 kind: Deployment metadata: n ...
- Request&Response总结
Request&Response Request 请求对象的类视图 请求对象常用方法 获取请求路径 返回值 方法名 说明 String getContextPath() 获取虚拟目录名称 St ...
- pycharm2021永久激活
Pycharm破解版地址: 链接: https://pan.baidu.com/s/1dEkzKRFMaeNjWF4h7y2TdQ 提取码: eqr3 Anaconda地址:版本是python3.6 ...
- Spring-01-事务
Spring事务机制 spring事务机制最重要的两个配置项,隔离级别和传播特性. 1. 隔离级别 隔离级别针对高并发问题导致的数据库丢失更新问题 1.1 数据库的4大基本特征 原子性(Atomic) ...
- Vue3 源码之 reactivity
注: 为了直观的看到 Vue3 的实现逻辑, 本文移除了边缘情况处理.兼容处理.DEV环境的特殊逻辑等, 只保留了核心逻辑 vue-next/reactivity 实现了 Vue3 的响应性, rea ...
- 给定HDFS中某一个目录,输出该目录下的所有文件的读写权限、大小、创建时间、路径等信息,如果该文件是目录,则递归输出该目录下所有文件相关信息。
1 import java.text.SimpleDateFormat; 2 import org.apache.hadoop.fs.*; 3 4 public class E_RecursiveRe ...