LOJ2507 CEOI2011 Matching
参考了 神仙yyb的博客
现在发现kmp不仅能匹配字符串,还可以用于处理任意模式匹配中的状态,如这题中已经匹配的序列中的数的大小关系就是一种状态,使用kmp找到模式序列的每一个前缀的border,即一个最长的前缀和后缀使得它们的数字大小关系相同,即匹配状态相同,然后在失配时跳转到一个前缀的状态继续匹配,就可以解决这个问题啦!
代码:
#include<bits/stdc++.h>
using namespace std;
#define N 1000007
int sum[N],n,m;
void modify(int x,int d)
{
for(;x<=m;x+=x&-x)
sum[x]+=d;
}
int query(int x)
{
int ans=;
for(;x;x-=x&-x)
ans+=sum[x];
return ans;
}
int read()
{
char c;
int x;
while((c=getchar())<||c>);
x=c-;
while((c=getchar())>=&&c<=)
x=x*+c-;
return x;
}
int rk[N],nxt[N],a[N],b[N],p[N];
vector<int> ans;
int main()
{
int i;
scanf("%d%d",&n,&m);
for(i=;i<=n;i++)
p[read()]=i;
for(i=;i<=n;i++)
{
modify(p[i],);
rk[i]=query(p[i]);
}
memset(sum,,sizeof(sum));
nxt[]=;
int last=;
for(i=;i<=n;i++)
{
int len=nxt[i-];
while(len&&query(p[i])+!=rk[len+])
{
len=nxt[len];
while(last<i-len)modify(p[last++],-);
}
if(query(p[i])+==rk[len+])
{
nxt[i]=len+;
modify(p[i],);
}
else nxt[i]=;
}
for(i=;i<=m;i++)
a[i]=b[i]=read();
sort(b+,b+m+);
for(i=;i<=m;i++)a[i]=lower_bound(b+,b+m+,a[i])-b;
memset(sum,,sizeof(sum));
int len=;
last=;
for(i=;i<=m;i++)
{
while(len&&query(a[i])+!=rk[len+])
{
len=nxt[len];
while(last<i-len)modify(a[last++],-);
}
if(query(a[i])+==rk[len+])
{
len++;
modify(a[i],);
if(len==n)ans.push_back(i-len+);
}
else len=;
}
int s=ans.size();
printf("%d\n",s);
for(i=;i<s;i++)
printf("%d ",ans[i]);
printf("\n");
return ;
}
LOJ2507 CEOI2011 Matching的更多相关文章
- 【LOJ#2507】[CEOI2011]Matching(KMP,树状数组)
[LOJ#2507][CEOI2011]Matching(KMP,树状数组) 题面 LOJ 题解 发现要做的是排名串的匹配. 然后我们考虑把它转成这个位置之前有多少个数小于当前这个数,这样子只要每个位 ...
- luoguP4696 [CEOI2011]Matching KMP+树状数组
可以非常轻易的将题意转化为有多少子串满足排名相同 注意到$KMP$算法只会在当前字符串的某尾添加和删除字符 因此,如果添加和删除后面的字符对于前面的字符没有影响时,我们可以用$KMP$来模糊匹配 对于 ...
- @loj - 2507@ 「CEOI2011」Matching
目录 @description@ @solution@ @accepted code@ @details@ @description@ 对于整数序列 \((a_1, a_2, ..., a_n)\) ...
- 学习《Hardware-Efficient Bilateral Filtering for Stereo Matching》一文笔记。
个人收藏了很多香港大学.香港科技大学以及香港中文大学里专门搞图像研究一些博士的个人网站,一般会不定期的浏览他们的作品,最近在看杨庆雄的网点时,发现他又写了一篇双边滤波的文章,并且配有源代码,于是下载下 ...
- LeetCode题解-----Wildcard Matching
题目描述: '?' Matches any single character. '*' Matches any sequence of characters (including the empty ...
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:annotation-driven'.
spring 配置文件报错报错信息:cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be ...
- [LeetCode] Wildcard Matching 外卡匹配
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- [LeetCode] Regular Expression Matching 正则表达式匹配
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- Beginning Scala study note(5) Pattern Matching
The basic functional cornerstones of Scala: immutable data types, passing of functions as parameters ...
随机推荐
- Intellij Idea 自动更新资源和类
Run -> Edit Configurations 进入配置页 On update action:当发现更新时的操作 选择Update classes and resources On ...
- Unity3d—GUI能量条
1.打开Unity编辑器. 2.在脚本文件夹中添加C#脚本,我的是添加了skill_01这个脚本.(要自己设置文件夹,方便管理,不然文件添乱不方便管理) 3.注意,脚本的名字一旦确定就不要去改动,因为 ...
- Redis(九)高可用专栏之Sentinel模式
本文讲述Redis高可用方案中的哨兵模式--Sentinel,RedisClient中的Jedis如何使用以及使用原理. Redis主从复制 Redis Sentinel模式 Jedis中的Senti ...
- linux 通过tar直接打包方式 迁移oracle的软件包环境:rdbms/lib/config.c
step1.修改sysctl.conf step2.修改ulimit.conf step3.打包源oracle安装包 step4.通过id{oracle的安装运行用户} 获取安装oracle的[ORA ...
- JS中判断是中文数字的函数
function checkcnnum(str) { ; const zh = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九','十','百','千 ...
- jQuery实现C#CheckBoxList模糊搜索
前言 最近开发的一套系统中需要对商品进行管理,在选择商品时,要分别从品牌.型号.商品三个类别分别选择对应的选项才能找到需要的商品,三者的关系为:品牌包含型号,型号包含商品,因此使用了三个不同的 asp ...
- 让你的 vs code 跑在云上,用手机浏览器就能写代码
让你的vs code 跑在云服务器上 在B站上看到一个视频Run VS Code in the browser with massive computing resources(教你如何配置一个云ID ...
- loadrunner:Action.c(4): Error -27796: Failed to connect to server "10.8.251.101:10086": [10060] Connection timed out
Action.c(4): Error -27796: Failed to connect to server "10.8.251.101:10086": [10060] Conne ...
- 分享几套2019年各大公司最新的PHP面试题,几斤几两一试便知
从面试题发现不足,进而查漏补缺,比通过面试更难得 PHP面试题2019年奇虎360面试题和答案解析 PHP面试题2019年京东工程师面试题和答案解析 PHP面试题2019年新浪工程师面试题和答案解析 ...
- 斐波那契查找(Fibonacci Search)
斐波那契查找 斐波那契查找就是在二分查找的基础上根据斐波那契数列进行分割的. 在斐波那契数列找一个等于略大于查找表中元素个数的数F[n],将原查找表扩展为长度为F[n](如果要补充元素,则补充重复 ...