【悬赏关注】题解区思路大致一致,但可能在求解 last 和 fst 数组时出错
#include <iostream>
#include <cstring>
#define int long long
using namespace std;
const int N = 2e5 + 100;
const int M = 26;
int n, m, ans;
char s[N], t[N];
int dp[M][2];
int P[N];
signed main()
{
memset (dp, -1, sizeof dp);
cin >> n >> m;
scanf("%s %s", s + 1, t + 1);
for (int i = 0; i < 26; i++)
{
int j;
j = 1;
while (s[j] != (char)('a' + i) && j <= n) j++;
dp[i][0] = j;
j = n;
while (s[j] != (char)('a' + i) && j >= 1) j--;
dp[i][1] = j;
}
for (int i = 1; i < m; i++)
ans = max(ans, dp[t[i + 1] - 'a'][1] - dp[t[i] - 'a'][0]);
cout << ans << endl;
system("pause");
return 0;
}