先翻转数组,然后用套kmp模板
#include"cstdio"
#include"cstring"
#include"iostream"
#include"algorithm"
typedef long long LL;
const LL mod = 1e9+7;
using namespace std;
const int maxn = 1e6+9;
char s[maxn],t[maxn];
int nxt[maxn];
int n,m;
LL ans;
void build(char *str)
{
nxt[0] = -1;
int k = -1, j = 0;
while(j < m){
while(k!=-1 && str[k] != str[j]) k = nxt[k];
nxt[++j] = ++k;
}
}
void work()
{
ans = 0;
LL j = 0;
int cnt = 0;
for(int i = 0; i <= n; i++){
while(j != -1 && s[i] != t[j]){
ans = (ans + (j+1)*j/2)%mod;
j = nxt[j];
}
++j;
if(j >= m){
cnt++;
ans = (ans+(j+1)*j/2)%mod;
j = nxt[j];
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%s%s",s,t);
n = strlen(s);
m = strlen(t);
reverse(s,s+n); reverse(t,t+m);
build(t);
work();
cout<<ans<<endl;
}
return 0;
}