A Secret

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

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.

 
Source
题目容易理解,匹配个数
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define ios() ios::sync_with_stdio(false)
#define INF 1044266558
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
const int maxn=1e6+;
ll t,la,lb,i,j,k;
char sa[maxn],sb[maxn];
ll nex[maxn],val[maxn];
void getnext()
{
memset(nex,,sizeof(nex));
memset(val,,sizeof(val));
nex[]=-;
for(i=;i<lb;i++)
{
j=nex[i];
val[i+]=i+;
while(j>-)
{
if(sb[j]==sb[i]) {nex[i+]=j+,val[i+]+=val[j+],val[i+]%=MOD;break;}
j=nex[j];
}
}
/*for(i=1;i<=lb;i++)
{
printf("%lld ",val[i]);
}
printf("\n");*/
}
ll matchfind()
{
ll ans=;
k=;
for(i=;i<la;i++)
{
j=k;
for(k=;j>=;j=nex[j])
{
if(sa[i]==sb[j])
{
k=j+;
break;
}
}
ans=(ans+val[k])%MOD;
}
return ans;
}
int main()
{
scanf("%lld",&t);
while(t--)
{
scanf("%s %s",sa,sb);
la=strlen(sa),lb=strlen(sb);
reverse(sa,sa+la),reverse(sb,sb+lb);
getnext();
printf("%lld\n",matchfind());
}
return ;
}
/*
a b a b b b a b a
next 0 0 1 2 0 0 1 2 3
val 1 2 4 6 5 6 8 10 13
valfj 1 2 3+1 4+2 5 6 7+1 8+2 9+4=9+3+1::next[9]=next[3]=next[1];
*/

HDU 6153 A Secret的更多相关文章

  1. HDU 6153 A Secret(扩展KMP模板题)

    A Secret Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others) Total ...

  2. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6153 A Secret KMP,思维

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6153 题意:给了串s和t,要求每个t的后缀在在s中的出现次数,然后每个次数乘上对应长度求和. 解法:关 ...

  3. HDU 6153 A Secret 套路,求解前缀在本串中出现的次数

    http://acm.hdu.edu.cn/showproblem.php?pid=6153 首先相当于翻转两个串,然后求s2前缀在s1中出现的次数. 这是一个套路啦 首先把两个串结合起来,中间加一个 ...

  4. 【kmp或扩展kmp】HDU 6153 A Secret

    acm.hdu.edu.cn/showproblem.php?pid=6153 [题意] 给定字符串A和B,求B的所有后缀在A中出现次数与其长度的乘积之和 A和B的长度最大为1e6 方法一:扩展kmp ...

  5. 2017中国大学生程序设计竞赛 - 网络选拔赛 1004 HDU 6153 A Secret (字符串处理 KMP)

    题目链接 Problem Description Today is the birthday of SF,so VS gives two strings S1,S2 to SF as a presen ...

  6. HDU 6153 A Secret(扩展kmp)

    A Secret Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)Total ...

  7. HDU 6153 A Secret (KMP)

    题意:给定两个串,求其中一个串 s 的每个后缀在另一个串 t 中出现的次数. 析:首先先把两个串进行反转,这样后缀就成了前缀.然后求出 s 的失配函数,然后在 t 上跑一遍,如果发现不匹配了或者是已经 ...

  8. HDU 6153 A Secret ( KMP&&DP || 拓展KMP )

    题意 : 给出两个字符串,现在需要求一个和sum,考虑第二个字符串的所有后缀,每个后缀对于这个sum的贡献是这个后缀在第一个字符串出现的次数*后缀的长度,最后输出的答案应当是 sum % 1e9+7 ...

  9. 【HDU 6153】A Secret (KMP)

    Problem Description Today is the birthday of SF,so VS gives two strings S1,S2 to SF as a present,whi ...

随机推荐

  1. Rain and Umbrellas(dp)

    题目链接 http://codeforces.com/problemset/problem/988/F 令dp[i][j]为走到目标为i处,手里拿着第j把伞,同时注意,在某处可能存在不止一把伞 #in ...

  2. vue的鼠标移入和移出

    vue的鼠标移入和移出 需求(鼠标到预约二维码显示,预约添加背景色) 实现 <!--html部分--> <ul class="person_list"> / ...

  3. mongodb 的查询深入剖析

           db.表名.find({goods_id:3});                     //查询出 goods_id 为 3 的数据        db.表名.find({cat_i ...

  4. top---实时动态地查看系统的整体运行情况

    top命令可以实时动态地查看系统的整体运行情况,是一个综合了多方信息监测系统性能和运行信息的实用工具.通过top命令所提供的互动式界面,用热键可以管理. 语法 top(选项) 选项 -b:以批处理模式 ...

  5. Eclipse反编译插件 Enhanced Class Decompiler

    因为jar包中的源码都是经过反编译的,所以需要安装插件才能查看到源码,此处介绍的是 Enhanced Class Decompiler 插件. 打开Eclipse,Help --> Eclips ...

  6. [Python] Boolean Or "Mask" Index Arrays filter with numpy

    NumPy Reference: Indexing Integer array indexing Boolean array indexing Note: The expression a < ...

  7. Go 语言编程

    [课程名称]Go 语言编程   [课程时间]2014年7月30日(周三) 20:50 - 22:00         [课程安排]20:50-21:00     通过邮件地址登录网络课堂       ...

  8. EBS TimeZone问题

    记录问题 :  http://blog.csdn.net/cymm_liu/article/details/29234919     --     Oracle 数据库中的 时间 时区 http:// ...

  9. Linux打包免安装的Qt程序(编写导出依赖包的脚本copylib.sh,程序启动脚本MyApp.sh)

    本文介绍如何打包Qt程序,使其在没有安装Qt的系统可以运行. 默认前提:另外一个系统和本系统是同一个系统版本. 1,编写导出依赖包的脚本copylib.sh #!/bin/bash LibDir=$P ...

  10. Task Scheduler

    https://technet.microsoft.com/en-us/library/cc748993(v=ws.11).aspx#BKMK_winui If Task Scheduler is n ...