Codeforces Round #579 (Div. 3) D2. Remove the Substring (hard version) ###K ###K //K

题目链接:https://codeforces.ml/contest/1203/problem/D2
题意:给定字符串a 问a最多删除多少个连续的子序列 还存在不连续子序列b
思路:考虑要删除的要么在两个相邻的子串字母之间 ,要么在左右两边

而相邻的最远 肯定是第一个出现和最后一个出现的比较 处理L R数组 L数组是b中第i个字符第一次出现的位置

R数组是b中第i个字符最后一次出现的位置

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 #define pb push_back
 5 const int maxn =2e5+10;
 6 const int mod=998244353;
 7 int n;
 8 int l[maxn];
 9 int r[maxn];
10 
11 
12 int main()
13 {
14     ios::sync_with_stdio(false);
15     cin.tie(0);
16     string a,b;
17     cin>>a>>b;
18     int la=a.size();
19     int lb=b.size();
20     int ans=0;
21     int cnt=0;
22     for(int i=1;i<=la;i++)
23     {
24         if(cnt<lb&&a[i-1]==b[cnt])
25         {
26             cnt++;
27             l[cnt]=i;
28         }
29     }
30     cnt=lb-1;
31     for(int i=la;i>=1;i--)
32     {
33         if(cnt>=0&&a[i-1]==b[cnt])
34         {
35             r[cnt+1]=i;
36             cnt--;
37         }
38     }
39     for(int i=1;i<lb;i++)
40     {
41         ans=max(ans,r[i+1]-l[i]-1);
42     }
43     ans=max(ans,r[1]-1);
44     ans=max(ans,la-l[lb]);
45     cout<<ans<<'\n';
46 
47 
48 
49 
50 }
View Code

 

posted @ 2020-10-23 23:29  canwinfor  阅读(62)  评论(0)    收藏  举报