日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Problem M. Mediocre String Problem(Z 函数 + PAM)

發(fā)布時間:2023/12/4 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Problem M. Mediocre String Problem(Z 函数 + PAM) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Problem M. Mediocre String Problem

給定兩個串s,ts, ts,t,要求有多少不同的三元組(i,j,k)(i, j, k)(i,j,k),滿足:

  • 1≤i≤j≤∣s∣1 \le i \le j \le \mid s \mid1ijs
  • 1≤k≤∣t∣1 \le k \le \mid t \mid1kt
  • j?i+1≥kj - i + 1 \ge kj?i+1k
  • s[i,j]+t[1,k]s[i, j] + t[1, k]s[i,j]+t[1,k]是一個回文串。

s[i,j]+t[1,k]s[i, j] + t[1, k]s[i,j]+t[1,k]連接而成的串為A+T+BA + T + BA+T+Bs[i,j]=A+T,t[1,k]=Bs[i, j] = A + T, t[1, k] = Bs[i,j]=A+T,t[1,k]=B,則一定滿足rev(A)=Brev(A) = Brev(A)=BTTT其本身是一個非空回文串。

我們考慮把sss串,翻轉,那么問題就變成了:

  • 1≤i≤j≤∣s∣1 \le i \le j \le \mid s \mid1ijs
  • 1≤k≤∣t∣1 \le k \le \mid t \mid1kt
  • j?i+1≥kj - i + 1 \ge kj?i+1k
  • s[i,j]+t[1,k]=T+A+Bs[i, j] + t[1, k] = T + A + Bs[i,j]+t[1,k]=T+A+B,其中T+A=s[i,j],B=t[1,k]T + A = s[i, j], B = t[1, k]T+A=s[i,j],B=t[1,k],滿足A=BA = BA=B,且TTT是一個非空回文串。

可以考慮用exkmpexkmpexkmp,得到sss的所有后綴與ttt中前綴的最長匹配長度,然后枚舉點iii,則答案為∑i=1mpalindrome_sum[i?1]×len[i]\sum\limits_{i = 1} ^{m} palindrome\_sum[i - 1] \times len[i]i=1m?palindrome_sum[i?1]×len[i]

palindrome_sum[i] 表示以 i 結尾的回文串有多少個,可以用回文樹,簡單求得。

所以只要套上EXKMP,PAMEXKMP, PAMEXKMP,PAM即可。

#include <bits/stdc++.h>using namespace std;namespace PAM { const int N = 1e6 + 10;int sz, tot, last, cnt[N], nex[N][26], len[N], fail[N], dep[N], palindrome_sum[N];char s[N];int node(int l) {++sz, len[sz] = l, fail[sz] = cnt[sz] = 0;return sz; }void init() {sz = -1, last = 0, s[tot = 0] = '$';node(0), node(-1), fail[0] = 1; }int getFail(int rt) {while (s[tot - len[rt] - 1] != s[tot]) {rt = fail[rt];}return rt; }void insert(char c, int id) {s[++tot] = c;int cur = getFail(last);if (!nex[cur][c - 'a']) {int x = node(len[cur] + 2);fail[x] = nex[getFail(fail[cur])][c - 'a'];nex[cur][c - 'a'] = x;}last = nex[cur][c - 'a'];cnt[last]++, dep[last] = dep[fail[last]] + 1;palindrome_sum[id] = dep[last]; } }const int N = 2e6 + 10;char str1[N], str2[N];int a[N], n, m;int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);scanf("%s %s", str2 + 1, str1 + 1);n = strlen(str1 + 1), m = strlen(str2 + 1);reverse(str2 + 1, str2 + m + 1);for (int i = 1; i <= m; i++) {str1[i + n] = str2[i];}for (int i = 2, l = 1, r = 1; i <= n + m; i++) {if (i <= r && a[i - l + 1] < r - i + 1) {a[i] = a[i - l + 1];}else {a[i] = max(0, r - i + 1);while (i + a[i] <= n + m && str1[a[i] + 1] == str1[i + a[i]]) {a[i]++;}}if (i + a[i] - 1 > r) {l = i, r = i + a[i] - 1;}}PAM::init();for (int i = 1; i <= m; i++) {PAM::insert(str2[i], i);}long long ans = 0;for (int i = n + 1; i <= n + m; i++) {ans += 1ll * PAM::palindrome_sum[i - n - 1] * min({n, n + m - i + 1, a[i]});}printf("%lld\n", ans);return 0; }

總結

以上是生活随笔為你收集整理的Problem M. Mediocre String Problem(Z 函数 + PAM)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。