目录

    • Codeforces Round #704 (Div. 2)-C. Maximum width
      • Problem Description
      • Input
      • Output
      • Sample Input
      • Sample Onput
      • Note
  • 题目大意
  • 题目分析
  • 解题思路
  • AC代码
  • 注意事项

Codeforces Round #704 (Div. 2)-C. Maximum width

传送门
Time Limit: 2 seconds
Memory Limit: 512 megabytes

Problem Description

Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: s s s of length n n n and t t t of length m m m.

A sequence p 1 , p 2 , … , p m p_1, p_2, \ldots, p_m p1​,p2​,…,pm​, where 1 ≤ p 1 < p 2 < … < p m ≤ n 1 \leq p_1 < p_2 < \ldots < p_m \leq n 1≤p1​<p2​<…<pm​≤n, is called beautiful, if s p i = t i s_{p_i} = t_i spi​​=ti​ for all i i i from 1 1 1 to m m m. The width of a sequence is defined as max ⁡ 1 ≤ i < m ( p i + 1 − p i ) \max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right) 1≤i<mmax​(pi+1​−pi​).

Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings s s s and t t t there is at least one beautiful sequence.

Input

The first input line contains two integers n n n and m m m ( 2 ≤ m ≤ n ≤ 2 ⋅ 1 0 5 2 \leq m \leq n \leq 2 \cdot 10^5 2≤m≤n≤2⋅105) — the lengths of the strings s s s and t t t.

The following line contains a single string s s s of length n n n, consisting of lowercase letters of the Latin alphabet.

The last line contains a single string t t t of length m m m, consisting of lowercase letters of the Latin alphabet.

It is guaranteed that there is at least one beautiful sequence for the given strings.

Output

Output one integer — the maximum width of a beautiful sequence.

Sample Input

5 3
abbbc
abc

Sample Onput

3

Note

In the first example there are two beautiful sequences of width 3 3 3: they are { 1 , 2 , 5 } \{1, 2, 5\} {1,2,5} and { 1 , 4 , 5 } \{1, 4, 5\} {1,4,5}.

In the second example the beautiful sequence with the maximum width is { 1 , 5 } \{1, 5\} {1,5}.

In the third example there is exactly one beautiful sequence — it is { 1 , 2 , 3 , 4 , 5 } \{1, 2, 3, 4, 5\} {1,2,3,4,5}.

In the fourth example there is exactly one beautiful sequence — it is { 1 , 2 } \{1, 2\} {1,2}.


题目大意

两个字符串s和t长度分别是n和m。
在s从左到右选一些字母组成t。
问s中所有选中的字符中,最大间距是多少。

题目保证能从s中选出t。


题目分析

要使间距最大,就要t中的某两个相邻的字母在s中选择时,第一个字母尽可能往前,第二个字母尽可能往后,这样才能使间距最大。

如s(abbbc)中选择t(abc),看t中相邻两个字母,先看aba要选的尽可能靠前(其实就一种选择),所以选第1个。b要选的尽可能靠后(可以选第2~4个),所以选第4个。这样的话它的最大间距就是4-3=3。同理看bc时,应分别选择第2个和第5个,间距同样是5-2=3。所以最大间距是3


解题思路

先从前往后遍历一遍,得到t中每个字母最靠前能选到s的第几个;
再从后往前遍历一遍,得到t中每个字母最靠后能选到s的第几个。
考虑所有t中相邻的两个字母,看看第一个字母最靠前,第二个字母最靠后,间距是多少。
更新最大间距,直到遍历完t。


AC代码

#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
char s[200010];
char t[200010];
int zuiQian[200010]; //最靠前能选到s中的第几个
int zuiHou[200010]; //最靠后能选到s中的第几个
int main()
{int n, m, M = 1;scanf("%d%d", &n, &m);scanf("%s", s);scanf("%s", t);int lastT = -1;for (int i = 0; i < m; i++){while (s[++lastT] != t[i]) //找到s中第一个没有选过的并且与t的下一个相同的字母。;zuiQian[i] = lastT;}lastT = n;for (int i = m - 1; i >= 0; i--){while (s[--lastT] != t[i]);zuiHou[i] = lastT;}for (int i = 1; i < m; i++){M = max(M, zuiHou[i] - zuiQian[i - 1]); //更新最大值}printf("%d\n", M);return 0;
}

注意事项

while (s[++lastT] != t[i]);是打比赛时确定s中下一个位置的简便写法。

学会的话打比赛会快亿点点,但是小心不要用错了哦。

Codeforces Round #704 (Div. 2)-C. Maximum width-题解相关推荐

  1. Codeforces Round #704 (Div. 2)-B. Card Deck-题解

    目录 Codeforces Round #704 (Div. 2)-B. Card Deck Problem Description Input Output Sample Input Sample ...

  2. Codeforces Round #704 (Div. 2)-A. Three swimmers-题解

    目录 Codeforces Round #704 (Div. 2)-A. Three swimmers Problem Description Input Output Sample Input Sa ...

  3. Codeforces Round #595 (Div. 3) F. Maximum Weight Subset 树形dp

    传送门 文章目录 题意: 思路: 题意: n≤200n\le200n≤200 思路: 明显的树形dpdpdp,所以考虑一下dpdpdp状态. 这个题状态挺神的..可能是因为我太菜了,看了半天才看懂. ...

  4. Codeforces Round #807 (Div. 2)A~E个人题解

    Dashboard - Codeforces Round #807 (Div. 2) - Codeforces A. Mark the Photographer 题意: 有个人,每个人的身高设为,现在 ...

  5. Codeforces Round #706 (Div. 2)-A. Split it!-题解

    目录 Codeforces Round #706 (Div. 2)-A. Split it! Problem Description Input Output Sample Input Sample ...

  6. Codeforces Round #704 (Div. 2)(A ~ E)5题全 超高质量题解【每日亿题2 / 23】

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 A.Three swimmers B.Card Deck C.Maximum width D.G ...

  7. Codeforces Round #704 (Div. 2) A-E题解

    A Three swimmers 题意 三个人每人游一个来回时间分别是a.b.c,那么在 a.b.c的倍数时间点上 三个人均会在左边的点,题目问你p时刻来 还要等多久最快遇到三个人 1e18 除法判断 ...

  8. Codeforces Round #742 (Div. 2) B、C 题解

    Codeforces Round 742 B. MEXor Mixup 题意 有一个数组,输入两个数a,b,a代表这个数组之外的最小非负整数,b代表这个数组的异或值,问你该数组的最小长度. 思路 首先 ...

  9. Codeforces Round #704 (Div. 2) E. Almost Fault-Tolerant Database 思维

    传送门 题意: 给nnn个长度为mmm的数组,要求构造一个长度为mmm的数组,使得这个数组与前面nnn个数组同一位置最多两个元素不同. 思路: 我们为了方便构造,可以先把要构造的数组看成nnn个数组的 ...

最新文章

  1. cn.hutool.poi.excel.ExcelUtil 只输出指定的标题
  2. 如何使用SSL pinning来使你的iOS APP更加安全
  3. 动态给组件添加背景,一半圆角
  4. MVP on Board 没用小技巧
  5. java面向对象程序设计董小园_java面向对象程序设计(董小园版).doc
  6. 在线图表编辑工具 draw.io 10.6.5 版本发布
  7. ORACLE 11G DATA GUARD配置之Dataguard基本原理
  8. 以一定概率执行某段代码(Python实现)
  9. 通过Keepalived实现Redis Failover自动故障切换功能[实践分享]
  10. 计算机二级C语言题库(60套真题+刷题软件)2022年9月份新题第二套
  11. cam350怎么看顶层_CAM350软件怎么查看gerber文件 cam350导出gerber教程
  12. [整理]ISO 27001信息安全管理体系审核员试题汇编之单项选择题
  13. 构建简单的智能客服系统(三)——基于 UniMRCP 实现讯飞 TTS MRCP Server
  14. JS编写 简易网页音乐播放器
  15. 前端JS时间验证,结束时间不早于开始时间
  16. 计算机设置从光盘启动怎么办,[光盘启动]BIOS设置从光盘光驱启动教程
  17. 精品Uniapp的餐厅餐馆饮订餐点餐管理系统实现的App
  18. 菜鸟学四轴控制器之6:刀具半径补偿算法
  19. 处理7z格式的001 002 文件
  20. ElectronBot支线项目

热门文章

  1. 暂缓海外建站、关停拍拍二手入口等调整后 重新聚焦电商业务的京东Q1财报亮眼...
  2. 实现屏幕固定位置截图
  3. 资讯--2018年12月与2018年年末总结
  4. 山东青岛至山西运城自驾回家过年记
  5. android制作9图片工具,Android设计必备神器——两张图教会你怎么快速制作点九图(.9.pn...
  6. 一款告诉你历史价格的插件,双11购物神器!
  7. 2023心理咨询师证怎么考 报考条件是什么
  8. 抖音矩阵系统,抖音SEO源码独立部署,look here
  9. 一篇文章讲透yolo v1-v7
  10. 项目前端(一)、vue项目中引入vue-router路由组件