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 ...
随机推荐
- Android开发系列(二十):AutoCompleteTextView(自己主动完毕文本框)的功能和使用方法
当用户输入一定的字符之后,自己主动完毕文本框可以显示一个下拉菜单,供用户从中选择,当用户选择某个菜单项之后,AutoCompleteTextView可以依照用户的选择自己主动填写该文本框 AutoCo ...
- jQuery笔记---选择器
查找API,jQuery选择器,定位标签 1.基本选择器 id定位标签 class属性定位标签 标签名定位标签 2.举例 <html> <head> <meta http ...
- Arcgis engine 指定图层对要素进行创建、删除等操作
Arcgis engine 指定图层创建点要素 在指定的图层上创建一个点要素,点要素的位置是通过X,Y坐标指定的,下面是具体的注释 .其中 和IFeatureClassWrite接口有关的代码不要好像 ...
- [Nuxt] Setup a "Hello World" Server-Rendered Vue.js Application with the Vue-CLI and Nuxt
Install: npm install -g vue-cli Init project: vue init nuxt/starter . Run: npm run dev Create a inde ...
- watchdog的正确使用方法
关于watchdog应该有过单片机学习经历的人.都比較熟悉.但watchdog的正确使用方法,恐怕大家假设没有经历过实际产品的开发不会有深入的理解. 瑞萨RL78系列的单片机自身带有watchdog, ...
- 2.CCGridAction(3D效果),3D反转特效,凸透镜特效,液体特效,3D翻页特效,水波纹特效,3D晃动的特效,扭曲旋转特效,波动特效,3D波动特效
1 类图组织 2 实例 CCSprite * spr = CCSprite::create("HelloWorld.png"); spr->setPosition(cc ...
- trident原理及编程指南
目录 trident原理及编程指南 一.理论介绍 1.trident是什么? 2.trident处理单位 3.事务类型 二.编程指南 1.定义输入流 2.统计单词数量 3.输出统计结果 4.split ...
- POJ 2642 The Brick Stops Here 0-1背包
poj: http://poj.org/problem?id=2642 大意: 给出n(n<=200)块黄铜合金,还有它们的浓度和价钱.给出若干个个询问使它们在n块中取 M 块 使得这M块合金的 ...
- centos7 安装部署运行 Redis5
原文:centos7 安装部署运行 Redis5 Redis5 下载与解压(官网: https://redis.io/download ) 下载命令:wget http://download.redi ...
- 键盘钩子监测按键后,获取键码及按键名称(MFC)
LRESULT CALLBACK LowLevelKeyboardProc(int nCode,WPARAM wParam,LPARAM lParam){ if(nCode ==HC_ACTION & ...