1. 程式人生 > >A Secret(拓展KMP模板題)

A Secret(拓展KMP模板題)

Problem DescriptionToday 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. 
InputInput 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.
OutputFor 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 Input2aaaaaaaabababababaSample Output1319Hintcase 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中出現的次數。