牛客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 ...
随机推荐
- LeetCode145 二叉树的后序遍历
给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? /** * Defin ...
- Linux调整lvm逻辑分区大小
转载自:https://www.cnblogs.com/kevingrace/p/5825963.html 个人记录一下 Linux下对lvm逻辑卷分区大小的调整(针对xfs和ext4不同文件系 ...
- Linux学习笔记 | 配置nginx
目录 一.Nginx概述 二.why Nginx? 三.Linux安装Nginx APT源安装 官网源码安装 四.nginx相关文件的配置 html文件:/var/www/html/index.htm ...
- python列表字符串集合常用方法
1.1 列表常用方法 # 1. append 用于在列表末尾追加新的对象a = [1,2,3]a.append(4) # the result : [1,2,3,4]# 2. count方法统计某个 ...
- 2019 Java开发利器Intellij IDEA安装、配置和使用
进入Intellij IDEA的官网,选择电脑对应的合适版本进行下载,这儿我选择的是Intellij IDEA的社区版,安装旗舰版可去网上找相应的教程. Intellij IDEA的官网:https: ...
- yum -y install gnuplot
[root@test~]# yum -y install gnuplotLoaded plugins: fastestmirrorLoading mirror speeds from cached h ...
- 【Linux】ethtool 用法
ethtool命令用于获取以太网卡的配置信息,或者修改这些配置.这个命令比较复杂,功能特别多. 语法 ethtool [ -a | -c | -g | -i | -d | -k | -r | -S | ...
- CTFHub - Web(五)
eval执行: 1.进入网页,显示源码, <?php if (isset($_REQUEST['cmd'])) { eval($_REQUEST["cmd"]); } els ...
- BAPI_MATERIAL_BOM_GROUP_CREATE创建BOM
天使用BAPI函数:BAPI_MATERIAL_BOM_GROUP_CREATE创建BOM组数据,过程中发现很多问题啊.总结如下: BOMGROUP VARIANTS 这两个参数必须要传 ...
- IP2188中文资料书
IP2188 是一款集成 12 种.用于 USB 输出端口的快充协议 IC,支持 USB 端口充电协议.支持 11种快充协议,包括 USB TypeC PD2.0/PD3.0/PPS DFP,HVDC ...