acdream1116 Gao the string!(扩展KMP)
今天是字符串填坑的一天,首先填的第一个坑是扩展KMP。总结一下KMP和扩展KMP的区别。
在这里s是主串,t是模式串。
KMP可以求出的是以s[i]为结尾的串和 t前缀匹配的最长的长度。假如这个长度是L的话,则:
s[i-L+1...i]=t[0...L]
而所谓的失配指针f[i]指的就是当前i点失配时要匹配的长度,实际是用t文本串去匹配t。
扩展KMP则是以s[i]为起始的串和 t前缀匹配的最长的长度。 假如这个长度的话,则:
s[i..i+L-1]=t[0...L]
扩展KMP里的nxt数组就是利用t本身和自己匹配达到的效果。所以nxt[i]就是以t的后缀i和t的前缀匹配的最长的长度,有了这个就可以用来求上次的这道题了。
自己写的时候写了个后缀数组的版本,可以将t的前缀理解成后缀0,于是就是后缀之间的最长前缀,就是利用lcp来求,无奈的是后缀数组本身求的速度太慢了,rmq的速度更慢。由于扩展KMP是线性的,所以这次就可以顺利的过了这道坑爹题了。
下面第一个网里有一些理论的证明,但KMP那部分和我学的不太一样,不知道是我搞错了还是版本不一样,然后第二个链接里的代码感觉写的可读性强一点,在这里存一下模板。
http://www.cnblogs.com/10jschen/archive/2012/09/03/2668149.html
http://www.cnblogs.com/kuangbin/archive/2012/08/27/2659246.html
#pragma warning(disable:4996)
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std; #define ll long long
#define mxs 1000000
#define mxt 100000
#define mod 1000000007 char s[mxs], t[mxt]; int nxt[mxt], ex[mxt]; // get the next array for t only
void getNext(char *t,int *nxt)
{
int m = strlen(t);
nxt[0] = m;
int j = 0;
while (j + 1 < m&&t[j] == t[j + 1]) j++;
nxt[1] = j;
int k = 1; int p, L;
for (int i = 2; i < m; i++)
{
p = nxt[k] + k - 1;
L = nxt[i - k];
if (i + L < p + 1) nxt[i] = L; // i+L<=p
else
{
j = max(0, p - i + 1);
while (i + j < m&&t[i + j] == t[0 + j])j++;
nxt[i] = j;
k = i;
}
}
} // get the next array for t, and get the ex array for s;
void getExtend(char *s, char *t, int *nxt, int *ex)
{
getNext(t, nxt);
int n = strlen(s), m = strlen(t);
int j = 0;
while (j < n&&j < m&&s[j] == t[j]) j++;
ex[0] = j;
int k = 0; int p, L;
for (int i = 1; i < n; i++){
p = ex[k] + k - 1;
L = nxt[i - k];
if (i + L < p + 1) ex[i] = L;
else{
j = max(0, p - i + 1);
while (i + j < n&&j < m&&s[i + j] == t[j]) j++;
ex[i] = j;
k = i;
}
}
} struct Matrix
{
ll a[2][2];
Matrix(){ memset(a, 0, sizeof(a)); }
}m; Matrix operator * (const Matrix &a, const Matrix &b){
Matrix ret;
for (int i = 0; i < 2; i++){
for (int j = 0; j < 2; j++){
for (int k = 0; k < 2; k++){
ret.a[i][j] += (a.a[i][k] * b.a[k][j]) % mod;
ret.a[i][j] %= mod;
}
}
}
return ret;
}
Matrix operator ^ (Matrix a, ll n){
Matrix ret;
for (int i = 0; i < 2; i++) ret.a[i][i] = 1;
while (n){
if (n & 1) ret = ret*a;
n >>= 1;
a = a*a;
}
return ret;
} ll cal(ll n)
{
m.a[0][0] = 0; m.a[0][1] = 1;
m.a[1][0] = 1; m.a[1][1] = 1;
m = m^n;
return m.a[0][1];
} int main()
{
while (~scanf("%s",s)){
getNext(s, nxt);
int n = strlen(s);
nxt[n] = 0;
ll ans = 0;
for (int i = n - 1; i >= 0; i--){
nxt[i] += nxt[i + 1];
ans += cal(nxt[i]);
ans %= mod;
}
printf("%lld\n", ans);
}
return 0;
}
acdream1116 Gao the string!(扩展KMP)的更多相关文章
- hdu3336 Count the string 扩展KMP
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- ZOJ 3587 Marlon's String 扩展KMP
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3587 题意:给出两个字符串S和T.S,T<=100000.拿出 ...
- HDU-3336-Count the string(扩展KMP)
链接: https://vjudge.net/problem/HDU-3336 题意: It is well known that AekdyCoin is good at string proble ...
- acdream1116 Gao the string!(hash二分 or 后缀数组)
问题套了一个斐波那契数,归根结底就是要求对于所有后缀s[i...n-1],所有前缀在其中出现的总次数.我一开始做的时候想了好久,后来看了别人的解法才恍然大悟.对于一个后缀来说 s[i...n-1]来说 ...
- [2019杭电多校第五场][hdu6629]string matching(扩展kmp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6629 题意求字符串的每个后缀与原串的最长公共前缀之和. 比赛时搞东搞西的,还搞了个后缀数组...队友一 ...
- 【string】KMP, 扩展KMP,trie,SA,ACAM,SAM,最小表示法
[KMP] 学习KMP,我们先要知道KMP是干什么的. KMP?KMPLAYER?看**? 正如AC自动机,KMP为什么要叫KMP是因为它是由三个人共同研究得到的- .- 啊跑题了. KMP就是给出一 ...
- 扩展KMP --- HDU 3613 Best Reward
Best Reward Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3613 Mean: 给你一个字符串,每个字符都有一个权 ...
- UVA5876 Writings on the Wall 扩展KMP
扩展KMP的简单题. #include<stdio.h> #include<string.h> #define maxn 51010 char s[maxn],t[maxn]; ...
- hdu4333 扩展KMP
慢慢研究可以发现,可以用扩展kmp来求.由于扩展kmp的next[]只有一部分,当前位子前面那部分和母串的后部分,所以可以将字符串复制接在后面一次. 先求如果next[]>0&& ...
随机推荐
- C# 页面抓取类
抓取网站页面的内容,简单的类应用,代码如下: /// <summary> /// 获取页面内容 /// </summary> /// <param name=" ...
- hdu 1029
#include"stdio.h" int main(void) { int n,x,y,t,i; while(scanf("%d",&n)!=EOF) ...
- spring与mysql整合数据源的配置
需要解决两点,数据源的配置交给spring完成,事务管理交个spring来管理. <context:property-placeholder location="classpath:c ...
- Android操作系统11种传感器介绍
我们依次看看这十一种传感器 1 加速度传感器 加速度传感器又叫G-sensor,返回x.y.z三轴的加速度数值. 该数值包含地心引力的影响,单位是m/s^2. 将手机平放在桌面上,x轴默认为0,y轴默 ...
- win10里安装.net3.5
在CMD窗口里面输入这样一段,来安装Microsoft.MET Framework 3.5 Dism /online /enable-feature /featurename:NetFX3 /All ...
- sublime3快捷键
Ctrl+D 选中光标所占的文本,继续操作则会选中下一个相同的文本. Alt+F3 选中文本按下快捷键,即可一次性选择全部的相同文本进行同时编辑.举个栗子:快速选中并更改所有相同的变量名.函数名等. ...
- with check option
通过有with check option选项的视图操作基表(只是面对单表,对连接多表的视图正在寻找答案),有以下结论:首先视图只操作它可以查询出来的数据,对于它查询不出的数据,即使基表有,也不可以通过 ...
- find用法
find - 递归地在层次目录中处理文件 总 find [path...] [expression] 描 这个文档是GNU版本 find 命令的使用手册. find 搜索目录树上的每一个文件名,它从左 ...
- 不同系统间传输float型数据
#include <stdio.h> #include <string.h> int main(void) { union result { float d; ...
- C++中数组求偏移量计算公式
已知数组:type A[10][5]A[0][0] --A[8][4]面试常考:数组定义A[0....x][0...y]已知A[m][n] --求A[k][l]的地址: &A[m][n] ...