HDU 6153 拓展KMP (2017CCPC)

本文介绍了一种利用扩展KMP算法解决特定字符串匹配问题的方法,即求解一个字符串的所有后缀在另一个字符串中出现的次数与后缀长度乘积之和,通过逆序字符串将问题转化为前缀匹配问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

A Secret

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)
Total Submission(s): 975    Accepted Submission(s): 372

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
 
13
19
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.
 
 
题意

给你两个字符串A,B,现在要你求B串的后缀在A串中出现的次数和后缀长度的乘积和为多少。

思路

扩展KMP模板题,将s和t串都逆序以后就变成了求前缀的问题了,扩展KMP求处从i位置开始的最长公共前缀存于数组,最后通过将数组的值不为0的进行一个等差数列和的和就可以了。

 

代码:

 1 #include <iostream>
 2 #include <string>
 3 #include <string.h>
 4 #include <cstring>
 5 #include <algorithm>
 6 using namespace std;
 7 const int maxn = 1e6 + 10;
 8 const int mod = 1e9 + 7;
 9 typedef long long ll;
10 int cnt[maxn];
11 char A[maxn],B[maxn];
12 int Next[maxn],ex[maxn];
13 ll add(ll n)
14 {
15     ll m=((n%mod)*((n+1)%mod)/2)%mod;
16     return m;
17 }
18 void kmp(char P[])
19 {
20     int m=strlen(P);
21     Next[0]=m;
22     int j=0,k=1;
23     while(j+1<m&&P[j]==P[j+1]) j++;
24     Next[1]=j;
25     for(int i=2; i<m; i++)
26     {
27         int p=Next[k]+k-1;
28         int L=Next[i-k];
29         if(i+L<p+1) Next[i]=L;
30         else
31         {
32             j=max(0,p-i+1);
33             while(i+j<m&&P[i+j]==P[j])
34                 j++;
35             Next[i]=j;
36             k=i;
37         }
38     }
39 }
40 
41 void exkmp(char P[],char T[])
42 {
43     int m=strlen(P),n=strlen(T);
44     kmp(P);
45     int j=0,k=0;
46     while(j<n&&j<m&&P[j]==T[j])
47         j++;
48     ex[0]=j;
49     for(int i=1; i<n; i++)
50     {
51         int p=ex[k]+k-1;
52         int L=Next[i-k];
53         if(i+L<p+1)
54             ex[i]=L;
55         else
56         {
57             j=max(0,p-i+1);
58             while(i+j<n&&j<m&&T[i+j]==P[j])
59                 j++;
60             ex[i]=j;
61             k=i;
62         }
63     }
64 }
65 
66 int main()
67 {
68     int t;
69     scanf("%d",&t);
70     while(t--)
71     {
72         scanf("%s%s",A,B);
73         int lenA=strlen(A);
74         int lenB=strlen(B);
75         reverse(A,A+lenA);
76         reverse(B,B+lenB);
77         kmp(B);
78         memset(Next,0,sizeof(Next));
79         memset(ex,0,sizeof(ex));
80         exkmp(B,A);
81         ll ans = 0;
82         for(int i=0;i<lenA;i++)
83         {
84             if(ex[i])
85                 ans=(ans+add(ex[i])%mod)%mod;
86         }
87         printf("%lld\n",ans%mod);
88     }
89     return 0;
90 }

 

转载于:https://www.cnblogs.com/mj-liylho/p/7400064.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值