2017 CCPC网络赛 A Secret

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的后缀出现的次数*各自的长度%1e9+7,的结果是多少;

    他问的是后缀,然后我们把a,b全部反转后,就会变成前缀的查询了,这时我们用扩展kmp,得到extand数组,表示a在i位开始匹配b的时候能够匹配的最大的前缀和,这样每一位上的数t就代表着有t个前缀在i位上匹配的话可以匹配,所以在当前位置上,ans+=t*(t+1);最后别忘了%1e9+7.

 

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 1000005;
const int mod = 1e9 + 7;
int extand[maxn], nex[maxn];
char a[maxn], b[maxn];
void pre_ekmp(char x[], int m, int nex[]) {
	int j = 0, k = 1;	
	nex[0] = m;
	while (j + 1 < m&&x[j] == x[j + 1]) j++;
	nex[1] = j;
	for (int s = 2; s < m; s++) {
		int p = nex[k] + k - 1;
		int l = nex[s - k];
		if (s + l < p + 1)nex[s] = l;
		else {
			j = max(0, p - s + 1);
			while (s + j < m&&x[s + j] == x[j])j++;
			nex[s] = j; k = s;
		}
	}

}
void ekmp(char x[], int m, char y[], int n, int nex[]) {
	pre_ekmp(x, m, nex);
	int j = 0, k = 0;
	while (j < n&&j < m&&x[j] == y[j])j++;
	extand[0] = j;
	for (int s = 1; s < n; s++) {
		int p = extand[k] + k - 1;
		int l = nex[s - k];
		if (s + l < p + 1)extand[s] = l;
		else {
			j = max(0, p - s + 1);
			while (s + j < n&&j < m&&y[s + j] == x[j])j++;
			extand[s] = j;
			k = s;
		}
	}
}
int main() {
	int te;
	scanf("%d", &te);
	while (te--) {
		scanf("%s%s", a, b);
		int lena = strlen(a);
		int lenb = strlen(b);
		reverse(a, a + lena);
		reverse(b, b + lenb);
		ekmp(b, lenb, a, lena, nex);
		long long int ans = 0;
		for (int s = 0; s < lena; s++) {
			long long int t = extand[s];
			t = (t*(t + 1) / 2) % mod;
			ans += t; ans %= mod;
		}
		printf("%lld\n", ans);
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值