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+二分法+矩阵高速功率的更多相关文章

  1. [POJ 3735] Training little cats (结构矩阵、矩阵高速功率)

    Training little cats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9613   Accepted: 2 ...

  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), ...

  3. POJ 3070 Fibonacci(矩阵高速功率)

    职务地址:POJ 3070 用这个题学会了用矩阵高速幂来高速求斐波那契数. 依据上个公式可知,第1行第2列和第2行第1列的数都是第n个斐波那契数.所以构造矩阵.求高速幂就可以. 代码例如以下: #in ...

  4. HDU 2842 Chinese Rings(矩阵高速功率+递归)

    职务地址:HDU 2842 这个游戏是一个九连环的游戏. 如果当前要卸下前n个环.由于要满足前n-2个都卸下,所以要先把前n-2个卸下.须要f(n-2)次.然后把第n个卸下须要1次,然后这时候要卸下第 ...

  5. hdu 2243 考研绝望——复杂的文字(AC自己主动机+矩阵高速功率)

    pid=2243" target="_blank" style="">题目链接:hdu 2243 考研路茫茫--单词情结 题目大意:略. 解题思 ...

  6. poj 3744 Scout YYF I (可能性DP+矩阵高速功率)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5062   Accepted: 1370 Description YYF i ...

  7. POJ 3233 Matrix Power Series(矩阵高速功率+二分法)

    职务地址:POJ 3233 题目大意:给定矩阵A,求A + A^2 + A^3 + - + A^k的结果(两个矩阵相加就是相应位置分别相加).输出的数据mod m. k<=10^9.     这 ...

  8. NYOJ 300 &amp;&amp; hdu 2276 Kiki &amp; Little Kiki 2 (矩阵高速功率)

    pid=300">Kiki & Little Kiki 2 时间限制:5000 ms  |  内存限制:65535 KB 难度:4 描写叙述 There are n light ...

  9. HDU - 2294 Pendant (DP滚动数组降维+矩阵高速功率)

    Description On Saint Valentine's Day, Alex imagined to present a special pendant to his girl friend ...

随机推荐

  1. 常用到的Linux命令

    记录一下日常用到的Linux命令,就当做日志了 1.查看Linux 端口号  netstat -apn | grep 80 2.杀死进程   kill -s 9 pid (tomcat 启动不起来有可 ...

  2. SimpleDateFormat的使用问题

    今天对过去的代码进行重构,因为使用静态方法调用的原因,使用了一个静态的SimpleDateFormat,结果FindBug报错了,查看了一下,说是使用了静态的SimpleDateFormat对象. S ...

  3. [React Intl] Get locale value from intl injector

    Get 'injectIntl' from  'react-intl', it is a high order componet. We need to wrap our component into ...

  4. [Angular] The Select DOM Event and Enabling Text Copy

    When we "Tab" into a input field, we want to select all the content, if we start typing, i ...

  5. Storm新特性之Flux

    Storm新特性之Flux Flux是Storm版本号0.10.0中的新组件,主要目的是为了方便拓扑的开发与部署.原先在开发Storm拓扑的时候整个拓扑的结构都是硬编码写在代码中的,当要对其进行改动时 ...

  6. WCF学习笔记——WCF基础

    一 WCF与SOA SOA是一种通过为所有软件提供服务外观,并将这些服务的WSDL集中发布到一个地方的一种组织企业软件的方法.它通过使用明确定义的接口通过跨越边界传递消息来让多个自治的服务协同工作.S ...

  7. lettuce--Advanced Redis client

    redis官方提供的java client: git地址:https://github.com/mp911de/lettuceAdvanced Redis client for thread-safe ...

  8. MongoDbHelper 帮助类(下)

    对MongoDbHelper帮助类进行了一下整合,但是代码中一个方法需要将string类型转化为BsonValue类型一直出错.所以欢迎留言指正 using System; using System. ...

  9. ZOJ 2850和ZOJ 1414

    下午上数据结构,结果竟然没有新题.T T果断上OJ来水一发 ZOJ 2850   Beautiful Meadow 传送门http://acm.zju.edu.cn/onlinejudge/showP ...

  10. 8.2 Android灯光系统_led_class驱动

    android-5.0.2\hardware\libhardware\include\hardware\lights.h  //系统一些宏定义 android源码只带的灯光驱动在linux内核的dri ...