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

题解


将两个串反转,再进行普通的kmp匹配。

记录模式串的每个前缀(即反转前的后缀)出现的次数,考虑kmp的next数组,f[i]代表1i包含1f[i],那么ans[f[i]]+=ans[i];

参考代码

#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define ll long long
#define inf 1000000000
#define PI acos(-1)
#define REP(i,x,n) for(int i=x;i<=n;i++)
#define DEP(i,n,x) for(int i=n;i>=x;i--)
#define mem(a,x) memset(a,x,sizeof(a))
using namespace std;
ll read(){
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void Out(ll a){
if(a<0) putchar('-'),a=-a;
if(a>=10) Out(a/10);
putchar(a%10+'0');
}
const int N=1e6+10;
const int MOD=1e9+7;
char a[N],b[N];
int f[N];
ll ans[N];
void get_next(){
int m=strlen(b);
f[0]=f[1]=0;
for(int i=1;i<m;i++){
int j=f[i];
while(j&&b[i]!=b[j]) j=f[j];
f[i+1]=b[i]==b[j]?j+1:0;
}
}
void kmp_count(){
int n=strlen(a),m=strlen(b);
int j=0;
for(int i=0;i<n;i++){
while(j&&b[j]!=a[i]) j=f[j];
if(b[j]==a[i]) j++;
ans[j]++;
if(j==m){
j=f[j];
if(j>m) j=0;
}
}
}
int main(){
int n=read();
while(n--) {
scanf("%s%s",a,b);
reverse(b,b+strlen(b));
reverse(a,a+strlen(a));
memset(ans,0,sizeof(ans));
get_next();kmp_count();
int m=strlen(b);
for(int i=m;i>=1;i--) ans[f[i]]+=ans[i];
ll sum=0;
for(ll i=1;i<=m;i++){
sum=(sum+i*ans[i])%MOD;
}
printf("%lld\n",sum);
}
return 0;
}

【HDU 6153】A Secret (KMP)的更多相关文章

  1. 【hdu 5918】Sequence I(KMP)

    给定两个数字序列,求a序列中每隔p个构成的p+1个序列中共能匹配多少个b序列. 例如1 1 2 2 3 3 每隔1个的序列有两个1 2 3 kmp,匹配时每次主串往前p个,枚举1到p为起点. 题目 # ...

  2. 【HDU 4763】Theme Section(KMP)

    这题数据水的一B.直接暴力都能够过. 比赛的时候暴力过的.回头依照正法做了一发. 匹配的时候 失配函数 事实上就是前缀 后缀的匹配长度,之后就是乱搞了. KMP的题可能不会非常直接的出,可是KMP的思 ...

  3. 【HDU 4992】 Primitive Roots (原根)

    Primitive Roots   Description We say that integer x, 0 < x < n, is a primitive root modulo n i ...

  4. 【HDU 1533】 Going Home (KM)

    Going Home Problem Description On a grid map there are n little men and n houses. In each unit time, ...

  5. 【HDU - 2102】A计划(bfs)

    -->A计划 Descriptions: 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的 ...

  6. 【HDU 5839】Special Tetrahedron(计算几何)

    空间的200个点,求出至少四边相等,且其余两边必须不相邻的四面体的个数. 用map记录距离点i为d的点有几个,这样来优化暴力的四重循环. 别人的做法是枚举两点的中垂面上的点,再把到中点距离相等的点找出 ...

  7. 【HDU 4445】Crazy Tank(暴力)

    高中物理斜抛运动,简单分析一下角度固定下来则可以计算每个cannonball的降落坐标lnd. 因此暴力计算不同角度下的结果. #include <cstdio> #include &qu ...

  8. 【HDU 4343】Interval query(倍增)

    BUPT2017 wintertraining(15) #8D 题意 给你x轴上的N个线段,M次查询,每次问你[l,r]区间里最多有多少个不相交的线段.(0<N, M<=100000) 限 ...

  9. 【HDU 6008】Worried School(模拟)

    Problem Description You may already know that how the World Finals slots are distributed in EC sub-r ...

随机推荐

  1. @ConfigurationProperties和@EnableConfigurationProperties配合使用

    https://blog.csdn.net/u010502101/article/details/78758330 @ConfigurationProperties注解主要用来把properties配 ...

  2. excel导入phpmyadmin

    1.将excel文件另存为txt文件,再将txt文件保存为.csv文件同时修改编码为UTF8 2.登录phpmyadmin,在phpmyadmin中创建好表格,按excel中的顺序创建每列 3.因为p ...

  3. [JLOI2008]将军

    Description 刘先生最近在学习国际象棋,使用一个叫"jloi-08"的游戏软件.在这个游戏里,不但可以和电脑普通地对弈,还可以学习著名的棋局,还有针对初学者的规则指导等丰 ...

  4. fzu 2204 7 dp

    题目链接: fzu 2204 7 题目描述: 给出n个小球,每个小球只能涂黑色或者是白色,七个连续的不能是同种颜色,问有多少种涂色方法? 解题思路: 刚开始没有考虑到是环形的,WA的风生水起,怪我咯! ...

  5. DP Codeforces Round #303 (Div. 2) C. Woodcutters

    题目传送门 /* 题意:每棵树给出坐标和高度,可以往左右倒,也可以不倒 问最多能砍到多少棵树 DP:dp[i][0/1/2] 表示到了第i棵树时,它倒左或右或不动能倒多少棵树 分情况讨论,若符合就取最 ...

  6. 01_C++学习笔记_入门

    1.float类型只能表示数字里面的前6位或者前7位.也就是说c++只能保证float类型的数字的前6位是正确的.如果要求的精度更高的话,请使用double和long double. float精度是 ...

  7. jQuery在$(function(){})中調用函數

    任務太緊,很少記筆記,記下一篇jQuery中調用函數的例子: 該方法是在載入頁面的時候,判斷 ModelName 不為空,則獲取Model信息加載到Table中: 另外,在點擊半成品編號文本框時,也調 ...

  8. let块级引起的闭包思考

    因为es6在node中用的比较频繁,最近在按计划根据阮一峰的es6教程从头开始学习一遍, 第一步遇到的就是“看似非常熟悉”的let小伙伴,核心character如下: 即:let变量的作用域只在块内. ...

  9. 【学习笔记】深入理解js原型和闭包(15)——闭包

    前面提到的上下文环境和作用域的知识,除了了解这些知识之外,还是理解闭包的基础. 至于“闭包”这个词的概念的文字描述,确实不好解释,我看过很多遍,但是现在还是记不住. 但是你只需要知道应用的两种情况即可 ...

  10. 计算1至n的k次方的和

    package com.ywx.count; import java.util.Scanner; /** * @author Vashon * date:20150410 * 题目:计算1至n的k次方 ...