(dp)Maximum width

Maximum width

$如何在m个点中找到最大的间隔? $

  • \(预处理l[],r[]分别表示第i个点最左可以在哪和最右可以在拿\)
  • \(ans=max(ans,r[i+1]-l[i])\)
#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PII pair<int, int>
#define x first
#define y second
#define inf 0x3f3f3f3f
const int N = 2e5 + 7;
int l[N], r[N];

int main() {
    IO;
    int n, m;
    cin >> n >> m;
    string sa, sb;
    cin >> sa >> sb;
    int nw = 0;
    for (int i = 0; i < n; ++i) 
        if (nw < m && sa[i] == sb[nw]) {
            l[nw] = i;
            nw++;
        }
    nw = m - 1;
    for (int i = n - 1; i >= 0; --i) 
        if (nw >= 0 && sa[i] == sb[nw]) {
            r[nw] = i;
            nw--;
        }
    int ans = 0;
    for (int i = 1; i < m; ++i)
        ans = max(ans, r[i] - l[i - 1]);
    cout << ans << '\n';
    return 0;
}
posted @ 2021-02-23 23:23  phr2000  阅读(65)  评论(0编辑  收藏  举报