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

题目链接:http://codeforces.com/contest/1492/problem/C

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

A sequence p1,p2,…,pm, where 1≤p1<p2<…<pm≤n, is called beautiful, if spi=ti for all i from 1 to m. The width of a sequence is defined as max1≤i<m(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 and t there is at least one beautiful sequence.

Input
The first input line contains two integers n and m (2≤m≤n≤2⋅105) — the lengths of the strings s and t.

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

The last line contains a single string t of length 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.

Examples
input

5 3
abbbc
abc

output

3

input

5 2
aaaaa
aa

output

4

input

5 5
abcdf
abcdf

output

1

input

2 2
ab
ab

output

1

分析

只要找到 t 串中每个字符在 s 串可能的位置中最靠坐的和最靠右的,然后都进行比较即可得到答案。

代码
#include<bits/stdc++.h>
using namespace std;
#define lli long long int

lli forw[200007];
lli back[200007];

int main(){
	lli n,m;
	cin>>n>>m;
	string s,t;
	cin>>s>>t;
	
	for(lli i=0,j=0;i<n && j<m;i++)
	{
		if(t[j]==s[i])
		{
			forw[j]=i;
			j++;
		}
	}
	for(lli i=n-1,j=m-1;i>0 && j>0;i--)
	{
		if(t[j]==s[i])
		{
			back[j]=i;
			j--;
		}
	}
	lli maxi=0;
	for(int i=0;i<m-1;i++)
	maxi=max(maxi,back[i+1]-forw[i]);
	
	cout<<maxi;
	return 0;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值