The Preliminary Contest for ICPC Asia Xuzhou 2019 M. Longest subsequence(思维+序列自动机)
序列自动机跑s串
假设k为s和t相同的长度,初始时相同长度为0
取s串中大于t[i]的最左边的位置,用n-tmp+1+i-1更新答案,tmp是最左端的位置
然后去t[i]相等的位置,走到下一位,如果下一位的位置不存在或者在tmp的右边,跳出循环即可。
最后就是s串中找出了一个和t串相同的串,之后的长度只要不为0,也是可以用来更新答案的。
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
const int INF=0x3f3f3f3f;
int up[30],nxt[maxn][27];
char s[maxn],t[maxn];
int n,m;
void getNext()
{
memset(up,INF,sizeof(up));
for (int i=n;i>=0;i--) {
for (int j=0;j<=25;j++) {
nxt[i][j]=up[j];
}
if (i!=0) {
up[s[i]-'a']=i;
}
}
}
int main()
{
scanf("%d%d",&n,&m);
scanf("%s",s+1);
scanf("%s",t+1);
int ans=-1;
getNext();
int tmp=INF;
int loc=0;
for (int i=1;i<=m;i++) {
tmp=INF;
for (int j=t[i]-'a'+1;j<=25;j++) {
tmp=min(nxt[loc][j],tmp);
}
if (tmp!=INF) {
ans=max(ans,n-tmp+1+i-1);
}
if (nxt[loc][t[i]-'a']==INF||nxt[loc][t[i]-'a']>tmp) {
break;
}
else {
loc=nxt[loc][t[i]-'a'];
}
if (i==m && m+n-loc>m) {
ans=max(ans,m+n-loc);
}
}
printf("%d\n",ans);
return 0;
}
/*
8 3
aabcdccc
abc
ans=7
*/
The Preliminary Contest for ICPC Asia Xuzhou 2019 M. Longest subsequence(思维+序列自动机)的更多相关文章
- 计蒜客 41391.query-二维偏序+树状数组(预处理出来满足情况的gcd) (The Preliminary Contest for ICPC Asia Xuzhou 2019 I.) 2019年徐州网络赛)
query Given a permutation pp of length nn, you are asked to answer mm queries, each query can be rep ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 E XKC's basketball team [单调栈上二分]
也许更好的阅读体验 \(\mathcal{Description}\) 给n个数,与一个数m,求\(a_i\)右边最后一个至少比\(a_i\)大\(m\)的数与这个数之间有多少个数 \(2\leq n ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019
A:Who is better? 题目链接:https://nanti.jisuanke.com/t/41383 题意: 类似于有N个石子,先手第一次不能拿完,每次后手只能拿 1 到 前一次拿的数量* ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 E. XKC's basketball team
题目链接:https://nanti.jisuanke.com/t/41387 思路:我们需要从后往前维护一个递增的序列. 因为:我们要的是wi + m <= wj,j要取最大,即离i最远的那个 ...
- 计蒜客 41387.XKC's basketball team-线段树(区间查找大于等于x的最靠右的位置) (The Preliminary Contest for ICPC Asia Xuzhou 2019 E.) 2019年徐州网络赛
XKC's basketball team XKC , the captain of the basketball team , is directing a train of nn team mem ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 【 题目:so easy】{并查集维护一个数的下一个没有被删掉的数} 补题ING
题意:给[1,n],n个数,有两种操作: 1 x,删去x2 x,查询还未被删去的数中大于等于x的最小的数是多少. input: output: 做法:按照并查集的方法压缩路径 代码: #include ...
- G.Colorful String(The Preliminary Contest for ICPC Asia Xuzhou 2019)
https://nanti.jisuanke.com/t/4 #include <bits/stdc++.h> using namespace std; ,; typedef unsign ...
- E.XKC's basketball team(The Preliminary Contest for ICPC Asia Xuzhou 2019)
https://nanti.jisuanke.com/t/41387 解: 离散化+线段树. #define IOS ios_base::sync_with_stdio(0); cin.tie(0); ...
- A.Who is better?(The Preliminary Contest for ICPC Asia Xuzhou 2019)
https://nanti.jisuanke.com/t/41383 解: 斐波那契博弈+中国剩余定理. #include <bits/stdc++.h> using namespace ...
随机推荐
- H5与native 普及
H5与native 普及: H5是基于web,native基于客户端native是使用原生系统内核的,相当于直接在系统上操作.,是我们传统意义上的软件,更加稳定.但是H5的APP先得调用系统的浏览器内 ...
- Manacher算法求最长回文串模板
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> ...
- NAND FLASH驱动框架以及程序实现
1.NAND FLASH的硬件连接: 实验用的NAND FLASH芯片为K9F2G08U0C,它是三星公司的存储芯片,它的大小为256M.它的接线图如下所示: 它的每个引脚的分别为LDATA0-LDA ...
- 题解 Fractal Streets
题目链接 参考博客 #include<cstdio> #include<math.h> #include<utility>//pair using namespac ...
- APP开发工具如何选?
随着技术的发展,在当前开发一款APP已经非常的简单和快速.特别是近些年,利用HTML5技术将APP的开发门槛进一步降低.各种开发工具和框架层出不穷,令人眼花缭乱.这么多的工具摆在眼前应该如何进行选择呢 ...
- spring(四):DI流程
在IoC容器初始化过程中,一般不包含Bean依赖注入的实现. 依赖注入一般发生在应用第一次向容器获取Bean时(getBean),但是有一个例外. 在使用IoC容器时有一个预实例化的配置,即通过laz ...
- AcWing 9. 分组背包问题
#include <iostream> #include <algorithm> using namespace std; ; int n, m; int v[N][N], w ...
- AcWing 874. 筛法求欧拉函数
#include<bits/stdc++.h> using namespace std; typedef long long ll; ; int primes[N],cnt; int ph ...
- Unity Coroutine详解(二)
• 介绍• Part 1. 同步等待• Part 2. 异步协程• Part 3. 同步协程• Part 4. 并行协程 1.介绍 ...
- 【转载】Java多线程
转自:http://www.jianshu.com/p/40d4c7aebd66 引 如果对什么是线程.什么是进程仍存有疑惑,请先Google之,因为这两个概念不在本文的范围之内. 用多线程只有一个目 ...