【悬赏关注】题解区思路大致一致
查看原帖
【悬赏关注】题解区思路大致一致
637788
kimi0705楼主2023/3/18 21:28

【悬赏关注】题解区思路大致一致,但可能在求解 lastlastfstfst 数组时出错

#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];
// dp[i][0] 表示 (char)('a' + i) 这个字符在字符串 s 中,第一次出现的位置;
// dp[i][1] 表示 (char)('a' + i) 这个字符在字符串 s 中,最后一次出现的位置;
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;
}
2023/3/18 21:28
加载中...