题目链接

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 = (33+32+4*1)%1000000007.

题意:

给定两个字符串,一个匹配串一个模式串,我们的目的是在匹配串中找出所有的能够以模式串为前缀的串的长度,将长度累加求得最终的长度。

分析:

这道题最核心的思想就是模式匹配,但是又有一点点的改进。

我们之大模式匹配在找到一个匹配串时候就直接逃过了,这样的话会减少我们中间查找的匹配串,所以我们将模式串中每一个字符上一次出现的位置记录下来,这样的话就能很好的解决这个问题。

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

代码:

#include <cstdio>
#include <iostream>
#include<string.h>
#include <algorithm>
#define ll long long
using namespace std;
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 T;
scanf("%d",&T);
while(T--)
{
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;
}

2017中国大学生程序设计竞赛 - 网络选拔赛 1004 HDU 6153 A Secret (字符串处理 KMP)的更多相关文章

  1. 2017中国大学生程序设计竞赛 - 网络选拔赛 1005 HDU 6154 CaoHaha's staff (找规律)

    题目链接 Problem Description "You shall not pass!" After shouted out that,the Force Staff appe ...

  2. 2017中国大学生程序设计竞赛 - 网络选拔赛 1003 HDU 6152 Friend-Graph (模拟)

    题目链接 Problem Description It is well known that small groups are not conducive of the development of ...

  3. HDU 6154 - CaoHaha's staff | 2017 中国大学生程序设计竞赛 - 网络选拔赛

    /* HDU 6154 - CaoHaha's staff [ 构造,贪心 ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 题意: 整点图,每条线只能连每个方格的边或者对角线 问面积大于n的 ...

  4. HDU 6150 - Vertex Cover | 2017 中国大学生程序设计竞赛 - 网络选拔赛

    思路来自 ICPCCamp /* HDU 6150 - Vertex Cover [ 构造 ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 题意: 给了你一个贪心法找最小覆盖的算法,构造一组 ...

  5. HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛

    普通的数位DP计算回文串个数 /* HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 2-36进制下回文串个数 */ ...

  6. HDU 6154 CaoHaha's staff(2017中国大学生程序设计竞赛 - 网络选拔赛)

    题目代号:HDU 6154 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6154 CaoHaha's staff Time Limit: 2000/1 ...

  7. 2016中国大学生程序设计竞赛 - 网络选拔赛 1004 Danganronpa

    Problem Description Chisa Yukizome works as a teacher in the school. She prepares many gifts, which ...

  8. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6155 Subsequence Count 矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6155 题意: 题解来自:http://www.cnblogs.com/iRedBean/p/73982 ...

  9. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6154 CaoHaha's staff(几何找规律)

    Problem Description "You shall not pass!"After shouted out that,the Force Staff appered in ...

随机推荐

  1. 安装virtualbox

    1.安装virtualbox https://www.virtualbox.org/ 2.安装centos6.6 3.配置网络 右键-->网络-->网卡2-->host-only c ...

  2. 笔记之远程桌面服务(RDS)

    Windows默认只能有2个用户同时通过RDP进行连接,非常不方便,于是借此机会学习了下Win2012R2的远程桌面配置.以下我把学习过程记录一下: 1. 最开始我觉得只需要安装“Remote Des ...

  3. 左连接,右连接和等值连接(left join,right join和inner join)

    left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join(等值连接) 只 ...

  4. Girls' research HDU - 3294(马拉车水题)

    题意: 求最长回文串 长度要大于等于2  且输出起点和终点  输出回文串字符 这个字符还是要以给出的字符为起点a 输出 解析: 分析一下s_new串就好了 #include <iostream& ...

  5. 【刷题】BZOJ 5418 [Noi2018]屠龙勇士

    www.lydsy.com/JudgeOnline/upload/noi2018day2.pdf Solution 将攻击的式子列出来,\(atk \times x-p \times y=a_i\) ...

  6. 【转】rt-thread的位图调度算法分析

    序言 期待读者 本文期待读者有C语言编程基础,后文中要分析代码,对其中的一些C语言中的简单语句不会介绍,但是并不要求读者有过多的C基础,比如指针和链表等不会要求太多,后面在分析代码时,会附带地介绍相关 ...

  7. 【linux之文件查看,操作,权限管理】

    一.shell如何处理命令 1.shell会根据在命令中出现的空格字符,将命令划分为多个部分 2.判断第一个字段是内部命令还是外部命令 内部命令:内置于shell的命令(shell builtin) ...

  8. 单点登录(四)-----遇到问题-----cas server 源码部署tomcat运行报错ClassNotFoundException: org.jasig.cas.CasEnvironmentCo

    情况 cas单点登录 cas server 源码部署tomcat运行报错 把cas server的代码下载下来后使用gradle插件或者maven插件以及转化成eclipse·后导入发现部署到tomc ...

  9. bzoj 1212: [HNOI2004]L语言 AC自动机+状压

    为什么这道题网上所有题解写的都是N*Len的trie树的暴力啊,4E的复杂度... 为什么暴力还跑这么快啊TAT.. 有一个O(Len)的做法就是先把AC自动机建出来,因为每个字典串的长度很小,所以我 ...

  10. Python之旅:字典

      Python数据类型 #作用:存多个值,key:value 存取,取值速度快 #定义:key必须是不可变类型,value可以是任意类型 字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个 ...