Acdreamoj1116(Gao the string!)弦hash+二分法+矩阵高速功率
Problem Description
give you a string, please output the result of the following function mod 1000000007

n is the length of the string
f() is the function of fibonacci, f(0) = 0, f(1) = 1...
a[i] is the total number of times any prefix appear in the suffix s[i....n-1].
(the prefix means s[0...i] )
解法:假设知道了num[i]表示i開始的后缀s[i....n]跟前缀s[1...]之间的公共的前缀,那么以i开头的后缀中就匹配了num[i]个前缀了
所以i这个后缀出现的前缀的数量实际上就是num[i] + num[i+1] + .. num[n]. 求出来之后高速幂求斐波那契数列对应项大小就可以。求lcp的时候是二分+hash;字符串hash中,seed为31(java库源代码中是这个数,应该是效果比較好的)
代码:
/******************************************************
* author:xiefubao
*******************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string.h>
//freopen ("in.txt" , "r" , stdin);
using namespace std; #define eps 1e-8
#define zero(_) (abs(_)<=eps)
const double pi=acos(-1.0);
typedef long long LL;
const int Max=100010;
const int INF=1000000007;
const int hashseed=31; LL seed[Max];
LL has[Max];
char s[Max];
LL num[Max];
int len=0;
struct matrix
{
LL num[2][2];
matrix()
{
memset(num,0,sizeof num);
}
};
matrix operator*(const matrix& a,const matrix& b)
{
matrix ans;
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
for(int k=0;k<2;k++)
ans.num[j][k]+=(a.num[j][i]*b.num[i][k])%INF;
return ans;
}
matrix operator^(matrix a,LL n)
{
matrix ans;
ans.num[0][0]=1;
ans.num[1][1]=1;
while(n)
{
if(n&1)
{
ans=ans*a;
}
a=a*a;
n>>=1;
}
return ans;
}
LL getans(LL t)
{
if(t==0)
return 0;
if(t<=2)
return 1;
matrix tool;
tool.num[0][0]=1;
tool.num[0][1]=1;
tool.num[1][0]=1;
tool=tool^(t-1);
return tool.num[0][0]%INF;
}
void init()
{
seed[0]=1;
for(int i=1; i<Max; i++)
seed[i]=(seed[i-1]*hashseed)%INF;
}
LL Hash(int i,int L)
{
return ((has[i]-has[i+L]*seed[L])%INF+INF)%INF;
}
bool OK(int i,int l)
{
return Hash(0,l)==Hash(i,l);
}
void getnum(int i)
{
int left=i,right=len-1;
while(left<=right)
{
int middle=(left+right)/2;
if(OK(i,middle-i+1))
left=middle+1;
else
right=middle-1;
}
num[i]=right-i+1;
}
void makehash()
{
for(int i=len-1;i>=0;i--)
{
has[i]=(has[i+1]*hashseed+s[i])%INF;
}
num[0]=len;
for(int i=1;i<len;i++)
{
getnum(i);
}
}
int main()
{
init();
while(scanf("%s",s)==1)
{
memset(has,0,sizeof has);
len=strlen(s);
makehash();
for(int i=len-1;i>=0;i--)
num[i]+=num[i+1];
LL ans=0;
for(int i=0;i<len;i++)
ans=(ans+getans(num[i]))%INF;
cout<<ans<<'\n';
}
return 0;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
Acdreamoj1116(Gao the string!)弦hash+二分法+矩阵高速功率的更多相关文章
- [POJ 3735] Training little cats (结构矩阵、矩阵高速功率)
Training little cats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9613 Accepted: 2 ...
- UVA 10870 - Recurrences(矩阵高速功率)
UVA 10870 - Recurrences 题目链接 题意:f(n) = a1 f(n - 1) + a2 f(n - 2) + a3 f(n - 3) + ... + ad f(n - d), ...
- POJ 3070 Fibonacci(矩阵高速功率)
职务地址:POJ 3070 用这个题学会了用矩阵高速幂来高速求斐波那契数. 依据上个公式可知,第1行第2列和第2行第1列的数都是第n个斐波那契数.所以构造矩阵.求高速幂就可以. 代码例如以下: #in ...
- HDU 2842 Chinese Rings(矩阵高速功率+递归)
职务地址:HDU 2842 这个游戏是一个九连环的游戏. 如果当前要卸下前n个环.由于要满足前n-2个都卸下,所以要先把前n-2个卸下.须要f(n-2)次.然后把第n个卸下须要1次,然后这时候要卸下第 ...
- hdu 2243 考研绝望——复杂的文字(AC自己主动机+矩阵高速功率)
pid=2243" target="_blank" style="">题目链接:hdu 2243 考研路茫茫--单词情结 题目大意:略. 解题思 ...
- poj 3744 Scout YYF I (可能性DP+矩阵高速功率)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5062 Accepted: 1370 Description YYF i ...
- POJ 3233 Matrix Power Series(矩阵高速功率+二分法)
职务地址:POJ 3233 题目大意:给定矩阵A,求A + A^2 + A^3 + - + A^k的结果(两个矩阵相加就是相应位置分别相加).输出的数据mod m. k<=10^9. 这 ...
- NYOJ 300 && hdu 2276 Kiki & Little Kiki 2 (矩阵高速功率)
pid=300">Kiki & Little Kiki 2 时间限制:5000 ms | 内存限制:65535 KB 难度:4 描写叙述 There are n light ...
- HDU - 2294 Pendant (DP滚动数组降维+矩阵高速功率)
Description On Saint Valentine's Day, Alex imagined to present a special pendant to his girl friend ...
随机推荐
- Mybatis的使用中的一些不太注意的技巧
以下就总结一下Mybatis的使用中的一些不太注意的技巧,算是Mybatis的总结笔 1.插入时主键返回 我们向数据库插入一条记录是,使用Mybatis的<insert>是无法返回插入的主 ...
- 互信息 & 卡方 - 文本挖掘
在做文本挖掘,特别是有监督的学习时,常常需要从文本中提取特征,提取出对学习有价值的分类,而不是把所有的词都用上,因此一些词对分类的作用不大,比如“的.是.在.了”等停用词.这里介绍两种常用的特征选择方 ...
- 每日技术总结:flex,选项卡,classList,
1.Flex布局子元素垂直居中 给父元素添加以下样式: .parent { display: flex; align-items: center; } 2.js面向对象的选项卡 见另一篇文章 js面向 ...
- NSString常见用法
1.创建常量字符串 NSString *str = @"Hello World!"; 2.创建空字符串,给予赋值 NSString *str = [[NSString alloc] ...
- [AngularFire 2] Push, remove, update
import { Injectable } from '@angular/core'; import {RealtimeService} from "../shared"; imp ...
- context.getSystemService的简单说明
在android开发过程中这种方法一定不会陌生,比方我们在获取WindowManager和LayoutInflater对象的时候就须要我们调用这种方法.这种方法在context这个抽象类的一个抽象方法 ...
- spark 编程教程
参考: 英文:https://spark.apache.org/docs/latest/programming-guide.html 中文:http://www.cnblogs.com/lujin ...
- Linux 下配置 Python IDE——Emacs
工欲善其事,必先利其器.Python作为高级语言,因为其简介.灵活已经被越来越多的程序员所青睐.在尝试了众多IDE之后,终于找到了自己的挚爱.废话少说,下面开始说一下如何在linux下安装配置Emac ...
- 14、USB摄像头(V4L2接口)的图片采集
参考网站http://www.cnblogs.com/surpassal/archive/2012/12/19/zed_webcam_lab1.html 一.一些知识 1.V4L和V4L2. V4L是 ...
- 关于重定向RedirectAttributes的用法(转)
原文地址:https://zhuanlan.zhihu.com/p/21353217?refer=pengsong-java RedirectAttributes 是Spring mvc 3.1版本之 ...