HDU 6153 A Secret(扩展KMP模板题)
A Secret
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)
Total Submission(s): 2523 Accepted Submission(s): 934
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.
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.
The answer may be very large, so the answer should mod 1e9+7.
case 2:
Suffix(S2,1) = "aba",
题目链接:HDU 6153
扩展KMP解决的是对于每一个每一个$S$的后缀$Suffix(i)$,它与$T$串最长的公共前缀是多少,即它与$T$串各自从头开始能匹配几个,一旦有一个不匹配即结束。
这题求的是每一个$S_2$的后缀出现在$S_1$有几次,那么我们可以把两个串都反过来变成求$S_2$的前缀在$S_1$出现过几次,这样就可以发现如果$S_2$的前缀$prefix(j)$与$S_1$的后缀$Suffix(i)$的最长公共前缀为$k$,那么包括$prefix(j)$以及$j$以前的前缀均在$S_1$的$i$处出现过一次,贡献为$(k+1)*k/2$,那么这样求一遍$extend[]$数组后统计答案即可
代码:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <bitset>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 1e6 + 7;
const LL mod = 1000000007LL;
char s[N], t[N];
int nxt[N], ex[N]; void getnxt(char T[], int len, int nxt[])
{
nxt[0] = len;
int a;
int p;
for (int i = 1, j = -1; i < len; i++, j--)
{
if (j < 0 || i + nxt[i - a] >= p)
{
if (j < 0)
p = i, j = 0;
while (p < len && T[p] == T[j])
p++, j++;
nxt[i] = j;
a = i;
}
else
nxt[i] = nxt[i - a];
}
}
void getextend(char S[], char T[], int ls, int lt, int extend[], int nxt[])
{
getnxt(T, lt, nxt); //得到next
int a;
int p; //记录匹配成功的字符的最远位置p,及起始位置a
for (int i = 0, j = -1; i < ls; i++, j--) //j即等于p与i的距离,其作用是判断i是否大于p(如果j<0,则i大于p)
{
if (j < 0 || i + nxt[i - a] >= p) //i大于p(其实j最小只可以到-1,j<0的写法方便读者理解程序),
{ //或者可以继续比较(之所以使用大于等于而不用等于也是为了方便读者理解程序)
if (j < 0)
p = i, j = 0; //如果i大于p
while (p < ls && j < lt && S[p] == T[j])
p++, j++;
extend[i] = j;
a = i;
}
else
extend[i] = nxt[i - a];
}
}
int main(void)
{
int T, i;
scanf("%d", &T);
while (T--)
{
scanf("%s%s", s, t);
int la = strlen(s);
int lb = strlen(t);
reverse(s, s + la);
reverse(t, t + lb);
getnxt(t, lb, nxt);
getextend(s, t, la, lb, ex, nxt);
LL ans = 0;
for (i = 0; i < la; ++i)
{
LL temp = (LL)ex[i] * ((LL)ex[i] + 1LL) / 2LL;
ans = (ans + temp) % mod;
}
printf("%I64d\n", ans);
}
return 0;
}
HDU 6153 A Secret(扩展KMP模板题)的更多相关文章
- hdu 1711 Number Sequence(KMP模板题)
我的第一道KMP. 把两个数列分别当成KMP算法中的模式串和目标串,这道题就变成了一个KMP算法模板题. #include<stdio.h> #include<string.h> ...
- HDU 2594 扩展kmp模板题
题目大意: 给定两个字符串,在第一个字符串中找到一个最大前缀作为第二个字符串的后缀 #include <iostream> #include <cstdio> #include ...
- hdu 2087 剪花布条 kmp模板题
也是kuangbin专题的 专题名字太长 不复制了…… 刚好数据结构也学了kmp 找一道题敲敲模板…… 暴力的字符串匹配是O(n*m)的时间复杂度 而kmp通过一个O(m)的预处理将字符串匹配的时间复 ...
- HDU 6153 A Secret(扩展kmp)
A Secret Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)Total ...
- A - A Secret -扩展KMP
题目大意: 给你两个字符串A,B,现在要你求B串的后缀在A串中出现的次数和后缀长度的乘积和为多少. 题解: 扩展KMP模板题,将A和B串都逆序以后就变成了求前缀的问题了,扩展KMP求处从i位置开始的最 ...
- HDU 1711 - Number Sequence - [KMP模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...
- kmp模板 && 扩展kmp模板
kmp模板: #include <bits/stdc++.h> #define PB push_back #define MP make_pair using namespace std; ...
- POJ Oulipo KMP 模板题
http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4 ...
- POJ Oulipo(KMP模板题)
题意:找出模板在文本串中出现的次数 思路:KMP模板题 #include<cstdio> #include<cstring> #include<cmath> #in ...
随机推荐
- python三大神器之迭代器
可迭代协议: 内部含有__iter__方法的值/变量都是可迭代的.可迭代类型和python语言之间的协议. 可迭代对象: iterable,内部包含__iter__()函数. 迭代器: iterato ...
- Leecode刷题之旅-C语言/python-136只出现一次的数字
/* * @lc app=leetcode.cn id=136 lang=c * * [136] 只出现一次的数字 * * https://leetcode-cn.com/problems/singl ...
- 函数名前加 & 符号的深入理解 C++
#include <iostream> using namespace std; int& test_str() { ; return a; //通过返回 a 的地址来进行 值的返 ...
- kettle 遇到 解决Incorrect integer value: '' for column 'id' at row 1 完美解决-费元星
最近自己在测试一个开源的程序,测试中发现.该程序都添加和更新的时候回出现 Incorrect integer value: '' for column 'id' at row 1类是的错误! 后来我自 ...
- 转:C#微信公众号开发之接收事件推送与消息排重的方法
本文实例讲述了C#微信公众号开发之接收事件推送与消息排重的方法.分享给大家供大家参考.具体分析如下: 微信服务器在5秒内收不到响应会断掉连接,并且重新发起请求,总共重试三次.这样的话,问题就来了.有这 ...
- TensorLayer 中文文档
TensorLayer 中文文档 好消息 我们获得了 ACM Multimedia (MM) 年度最佳开源软件奖. TensorLayer 是为研究人员和工程师设计的一款基于Google Tensor ...
- Borland和Micorsoft的对话(转载自月光软件网)
Borland与Microsoft关于Delphi的对话 Bear 1.Delphi较贵 一套Delphi的价格大约相当于两套Visual Studio ------------------- ...
- Linux 文件与目录管理命令
处理目录的常用命令 常见的处理目录的命令: ls: 列出目录 cd:切换目录 pwd:显示目前的目录 mkdir:创建一个新的目录,语法:mkdir [-mp] 目录名称 -m :配置文件的权限 -p ...
- 《python核心编程第二版》第1章练习
1–1. 安装 Python.请检查 Python 是否已经安装到你的系统上,如果没有,请下载并 安装它 略 1–2. 执行 Python.有多少种运行 Python 的不同方法?你喜欢哪一种?为什 ...
- (原创)不过如此的 DFS 深度优先遍历
DFS 深度优先遍历 DFS算法用于遍历图结构,旨在遍历每一个结点,顾名思义,这种方法把遍历的重点放在深度上,什么意思呢?就是在访问过的结点做标记的前提下,一条路走到天黑,我们都知道当每一个结点都有很 ...