生活随笔
收集整理的這篇文章主要介紹了
hdu 6153 A Secret kmp + dp
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
傳送門
文章目錄
題意:
給你兩個串a,ba,ba,b,讓你求對于bbb的每個后綴,設其長度為lenlenlen,其在aaa中出現的次數為cntcntcnt,那么他的貢獻為len?cntlen*cntlen?cnt,求每個后綴的貢獻和是多少。
n≤1e6n\le1e6n≤1e6
思路:
首先我們將a,ba,ba,b都reversereversereverse一下,讓后后綴就轉換成了前綴。
先考慮如何快速求出來bbb的某個前綴在aaa中出現的次數。首先需要知道我們可以通過dpdpdp快速算出來某個串的一個前綴在當前串出現的次數。那么我們可以將a,ba,ba,b結合起來,分割點加上一個沒有出現的字符,比如s=b+′?′+as=b+'*'+as=b+′?′+a,這樣在sss中求一遍dpdpdp,算出來的是bbb的某個前綴在a,ba,ba,b中出現的次數,再減去在bbb中出現的次數即可。
預處理完上面的數組之后,直接枚舉bbb的前綴計算即可。
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std
;
typedef long long LL
;
typedef unsigned long long ULL
;
typedef pair
<int,int> PII
;const int N
=4000010,mod
=1e9+7,INF
=0x3f3f3f3f;
const double eps
=1e-6;string s
,a
,b
;
int ne
[N
],f1
[N
],f2
[N
];int main()
{ios
::sync_with_stdio(false);cin
.tie(0); int _
; cin
>>_
;while(_
--) {cin
>>a
>>b
;reverse(a
.begin(),a
.end()); reverse(b
.begin(),b
.end());s
="1"+b
+"*"+a
;for(int i
=1;i
<=s
.length();i
++) ne
[i
]=0,f1
[i
]=f2
[i
]=1;for(int i
=2,j
=0;i
<s
.length();i
++) {while(j
&&s
[i
]!=s
[j
+1]) j
=ne
[j
];if(s
[i
]==s
[j
+1]) j
++;ne
[i
]=j
;}b
="1"+b
;for(int i
=(int)s
.length()-1;i
>=1;i
--) f1
[ne
[i
]]+=f1
[i
];for(int i
=1;i
<=b
.length();i
++) ne
[i
]=0;for(int i
=2,j
=0;i
<b
.length();i
++) {while(j
&&b
[i
]!=b
[j
+1]) j
=ne
[j
];if(b
[i
]==b
[j
+1]) j
++;ne
[i
]=j
;}for(int i
=(int)b
.length()-1;i
>=1;i
--) f2
[ne
[i
]]+=f2
[i
];LL ans
=0;for(int i
=1;i
<b
.length();i
++) ans
+=1ll*(f1
[i
]-f2
[i
])*i
%mod
,ans
%=mod
;printf("%lld\n",ans
); }return 0;
}
總結
以上是生活随笔為你收集整理的hdu 6153 A Secret kmp + dp的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。