序列自动机跑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(思维+序列自动机)的更多相关文章

  1. 计蒜客 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 ...

  2. 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 ...

  3. The Preliminary Contest for ICPC Asia Xuzhou 2019

    A:Who is better? 题目链接:https://nanti.jisuanke.com/t/41383 题意: 类似于有N个石子,先手第一次不能拿完,每次后手只能拿 1 到 前一次拿的数量* ...

  4. The Preliminary Contest for ICPC Asia Xuzhou 2019 E. XKC's basketball team

    题目链接:https://nanti.jisuanke.com/t/41387 思路:我们需要从后往前维护一个递增的序列. 因为:我们要的是wi + m <= wj,j要取最大,即离i最远的那个 ...

  5. 计蒜客 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 ...

  6. The Preliminary Contest for ICPC Asia Xuzhou 2019 【 题目:so easy】{并查集维护一个数的下一个没有被删掉的数} 补题ING

    题意:给[1,n],n个数,有两种操作: 1 x,删去x2 x,查询还未被删去的数中大于等于x的最小的数是多少. input: output: 做法:按照并查集的方法压缩路径 代码: #include ...

  7. 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 ...

  8. 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); ...

  9. A.Who is better?(The Preliminary Contest for ICPC Asia Xuzhou 2019)

    https://nanti.jisuanke.com/t/41383 解: 斐波那契博弈+中国剩余定理. #include <bits/stdc++.h> using namespace ...

随机推荐

  1. 简写函数字面量(function literal)

    如果函数的参数在函数体内只出现一次,则可以使用下划线代替: val f1 = (_: Int) + (_: Int) //等价于 val f2 = (x: Int, y: Int) => x + ...

  2. PolandBall and Forest

    PolandBall lives in a forest with his family. There are some trees in the forest. Trees are undirect ...

  3. 小匠第一周期打卡笔记-Task02

    一.文本预处理 预处理通常包括四个步骤: 读入文本 分词 建立字典,将每个词映射到一个唯一的索引(index) 将文本从词的序列转换为索引的序列,方便输入模型 读入文本: import collect ...

  4. DTW + python 矩阵操作 + debug

    1.  from here. diagonalReturn specified diagonals. diagflatCreate a 2-D array with the flattened inp ...

  5. HITCON-Training-Writeup

    HITCON-Training-Writeup 原文链接M4x@10.0.0.55 项目地址M4x's github,欢迎star~ 更新时间5月16 复习一下二进制基础,写写HITCON-Train ...

  6. python setup.py 安装和卸载 的正确姿势

    1.install python setup.py install --record files.txt 2. uninstall 删除这些文件 cat files.txt | xargs rm -r ...

  7. MonoBehaviour单例的另外一种省事的写法

    using UnityEngine; public class CommSystem: SingletonGeneric<CommSystem> { public static strin ...

  8. Pyarm的Pyqt的配置

    相关连接: Python PyQt 安装python3.4 x64到c盘根目录. 安装PyQt5-5.5.1-gpl-Py3.4-Qt5.5.1-x64.exe 安装pycharm-professio ...

  9. layui之laydate

    .点击年份马上关闭窗口并且赋值 html代码: <div class="layui-form-item"> <label class="layui-fo ...

  10. 图解SOAPUI解析WSDL文件

    本文链接:https://blog.csdn.net/qq_16234613/article/details/53143279 新建项目 添加WSDL文件 查看方法 查看XML格式 运行测试