A Secret(拓展KMP模板题)

Problem Description
Today is the birthday of SF,so VS gives two strings S1,S2 to SF as a present,which have a big secret.SF is interested in this secret and ask VS how to get it.There are the things that VS tell:
  Suffix(S2,i) = S2[i...len].Ni is the times that Suffix(S2,i) occurs in S1 and Li is the length of Suffix(S2,i).Then the secret is the sum of the product of Ni and Li.
  Now SF wants you to help him find the secret.The answer may be very large, so the answer should mod 1000000007. 
Input
Input contains multiple cases.
  The first line contains an integer T,the number of cases.Then following T cases.
  Each test case contains two lines.The first line contains a string S1.The second line contains a string S2.
  1<=T<=10.1<=|S1|,|S2|<=1e6.S1 and S2 only consist of lowercase ,uppercase letter.
Output
For each test case,output a single line containing a integer,the answer of test case.
  The answer may be very large, so the answer should mod 1e9+7. 
Sample Input
2
aaaaa
aa
abababab
aba
Sample Output
1319
Hint
case 2: Suffix(S2,1) = "aba",
Suffix(S2,2) = "ba",
Suffix(S2,3) = "a".
N1 = 3,N2 = 3,N3 = 4.
L1 = 3,L2 = 2,L3 = 1.
ans = (3*3+3*2+4*1)%1000000007.

题干:每组给你两个字符串s1和s2,求s2的所有后缀在s1中出现的频率,频率再乘以对应的后缀的长度,累加。

思路:首先将求s2的后缀问题变为求s2的前缀问题,即可以使用拓展kmp模板,只需将两个字符串反转即可。

对于拓展kmp的总结:https://blog.csdn.net/dyx404514/article/details/41831947

使用拓展kmp的模板后,我们所得的extend数组存储了s2和s1的所有后缀的最长公共前缀,即extend数组对应的字符串都是s2的前缀,即其对应字符串的所有子串都在s1中出现过一次(长度也已知),所以只需对extend数组的每一个数进行从一开始的等差数列求和,最终的得到的总和就是所求结果。

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#define mod 1000000007
using namespace std;
long long nxt[1000000],extend[1000000];
long long sum,n;
const int maxn=1e6+5;
char s1[maxn], s2[maxn];
void getnext(char *P)  //求next数组模板
    {
        int m = strlen(P);
        nxt[0] = m;
        int i = 0;
        while(P[i] == P[i+1]) ++i;
        nxt[1] = i;
        int id = 1;
        for(i = 2; i < m; ++ i)
        {
            if(nxt[i-id] + i < id + nxt[id]) nxt[i] = nxt[i-id];
            else
            {
                int j = nxt[id] + id - i;
                if(j < 0) j = 0;
                while(i+j < m && P[j] == P[j+i]) ++j;
                nxt[i] = j;
                id = i;
            }
        }
    }

    void getex(char *P, char *T)  //求extend数组模板
    {
        int m = strlen(P);
        int n = strlen(T);
        int i = 0;
        while(i < m && i < n && P[i] == T[i]) ++i;
        extend[0] = i;
        int id = 0;
        for(int i = 1; i < m; ++i)
        {
            if(nxt[i-id]+i < extend[id]+id) extend[i] = nxt[i-id];
            else
            {
                int j = extend[id] + id - i;
                if(j < 0) j = 0;
                while(i + j < m && j < n && P[j+i] == T[j]) ++j;
                extend[i] = j;
                id = i;
            }
        }
    }
int main()
{
	cin>>n;
	while(n--)
	{
		sum=0;
		scanf("%s%s",s1,s2);
		memset(nxt,0,sizeof(nxt));
		memset(extend,0,sizeof(extend));
		int n = strlen(s1);
        int m = strlen(s2);
        reverse(s1, s1 + n);  //反转s1和s2,使其可以用拓展kmp模板
        reverse(s2, s2 + m);
		getnext(s2);
		getex(s1,s2);
		for(int i=0;i<n;i++)
		{
			sum+=(extend[i]*(extend[i]+1)/2)%mod;   //等差数列求和
			sum%=mod;
		}
		cout<<sum<<endl;
	}
	return 0;
}



ΣLitiΣLiti,其中LiLiPP的后缀ii的长度,titi为它在TT中出现的次数。
ΣLitiΣLiti,其中LiLiPP的后缀ii的长度,titi为它在TT中出现的次数。
ΣLitiΣLiti,其中LiLiPP的后缀ii的长度,titi为它在TT中出现的次数。
阅读更多
换一批

没有更多推荐了,返回首页