码迷,mamicode.com
首页 > 其他好文 > 详细

[CodeForces1492C] Maximum Width

时间:2021-03-01 13:26:32      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:hat   scan   esc   HERE   str   scanf   and   while   fir   

description:
There are two strings \(a\), \(b\) with the length \(n\), and \(m\).
Find the Array <\(p_1, p_2, ..., p_m>\), such that \(a_{p_i}= b_i\), \(\max\{p_{i+1}-p_i\}\) is max.
solution:
Let array left mark the left-first find \(p_i\), right mark the right-first find \(p_i\).
The answer is :

\(\max_{i=1}^{m-1}\{right[i+1]- left[i]\}\)
code:

#include<cstdio>
const int N = 2e5 + 1;
const int inf = -1e9;
char a[N], b[N];
int left[N], right[N];
int main() {
	int n, m;
	scanf("%d%d%s%s", &n, &m, a, b);
	int last = -1;
	for (int i = 0; i < m; ++i) {
		while (a[++last] != b[i]);
		left[i] = last;
	}
	last = n;
	for (int i = m - 1; i >= 0; --i) {
		while (a[--last] != b[i]);
		right[i] = last;
	}
	int ans = inf;
	for (int i = 1; i < m; ++i) {
		int cur = right[i] - left[i - 1];
		if (cur > ans)ans = cur;
	}
	printf("%d\n", ans);
}

[CodeForces1492C] Maximum Width

标签:hat   scan   esc   HERE   str   scanf   and   while   fir   

原文地址:https://www.cnblogs.com/dwt2021/p/14458106.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!