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. bzoj5085: 最大 暴力 二分

    Code: #include<cstdio> #include<algorithm> #include<cstring> #include<iostream& ...

  2. form表单提交的时候,传过去的值是键值对的形式

    效果展示 第一种需求,点击input的时候,input的value发生改变 $('.group-wrapper input').click(function(){ $(this).val(0); // ...

  3. python BeautifulSoup 获取页面多个子节点中的各个节点的内容

    页面html格式为 <tr bgcolor="#7bb5de"><td style="border-bottom: 1px solid #C9D8AD& ...

  4. jquery validate验证规则重用

    当多个控件验证规则相同时,如何避免冗余代码并应用相同规则呢? [1st way. addMethod+addClassRules] 场景:维护学生档案时需要维护父母.监护人.紧急联系人的身份证号码,此 ...

  5. HDU 4398 Template Library Management (最优页面调度算法)

    中等偏易题.操作系统理论中的最优页面调度算法,贪心.当需要淘汰某个模版时,淘汰掉当前手中在最远的将来才会被用到(或者以后永远不再用到)的那个. 代码: #include <iostream> ...

  6. easyui combobox 获取焦点

    easyui combobox 获取焦点 学习了:http://blog.csdn.net/foart/article/details/14446809 可以直接用: $('#spanZhudaoci ...

  7. HDU44979 GCD and LCM (素因子分解+计数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4497 题意: 求有多少种(x,y,z)使得最小公倍数为l,最大公约数为g 分析: 我们将l,g进行素因 ...

  8. ubuntu12.04更新软件源时出现校验和不符

    在运行update命令之后.出现系统校验和不符.网上找了一些方法,最后在大神的帮助下最终攻克了! ! 1.更改 /etc/apt/apt.conf.d/00aptitude 文件,在最后一行增加: A ...

  9. vijos - P1176奇怪的数列 (递归 + 找规律)

    P1176奇怪的数列 Accepted 标签:[显示标签] 背景 一天.学军数学小组的成员遇到了一个奇怪的数列,正巧信息小组的你碰到了他们. 于是他们把这个数列展示给你-- 描写叙述 这个数列是这种: ...

  10. oracle_序列、索引、同义词

     ①序列 1.序列: 可供多个用户用来产生唯一数值的数据库对象     自己主动提供唯一的数值     共享对象     主要用于提供主键值     将序列值装入内存能够提高訪问效率 2.CREA ...