链接:https://vjudge.net/contest/348351#problem/B
题目:
给出一个字符串s,一个字符串t,字符串t是s的子序列,所以t可以通过删除s中的若干个子字符串tt得到,求出最长的tt的长度len。
思路:
前得到字符串t在字符串s中第一次出现的位置pos,然后遍历每个串的最后一个位置i,不断更新i-pos得到最终最长的tt的长度len。
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
char s1[N],s2[N];
int id[N];
int main(void)
{
scanf("%s%s",s1+1,s2+1);
int l1 = strlen(s1+1),l2 = strlen(s2+1),k = 0;
for(int i=1,j=1;i<=l1;i++)
{
if(j<=l2 && s1[i] == s2[j])
{
id[++k] = i;
j++;
}
}
int ans = 0;
for(int i=l1,j=l2;i>=1;i--)
{
ans = max(ans,i-id[j]);
if(j>=1 && s1[i] == s2[j]) j--;
}
printf("%d\n",ans);
return 0;
}