ACdream 1116 扩展KMP 线段树区间累加 Fib数列的矩阵加速

传送门:点击打开链接

Gao the string!

Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)
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] )

Input

multiple test cases.

each test case will give you a string consists of lowercase letters, the length of which is no more than 100000.

Output
ouput a number.
Sample Input
aa
Sample Output
3
Hint

样例解释如下:

对于

aa这个后缀,前缀a出现了两次,前缀aa出现了一次,总共三次

对于a这个后缀,前缀a出现了一次

所以答案是f(3) + f(1)

Source
wuyiqi


题意:给出一个长度小于10W的字符串。用a[I]表示所以前缀在后缀s[I..n]中出现的次数。求的值 mod 1000000007,f()为fib数列。

思路:求出字符串每一位的exnext值。(exnext的第一位在这道题强行定位s的长度)。用线段树维护a[I]的值,假设next[I]=k。则在后缀1到后缀I中,都出现了前缀1到前缀k。所以将区间[1,I]都加上exnext[I]。最后将a[I]都导出导到一个数组里面(不导出也可以)方便后面的计算。由于a[ii[]很大,可能超过int。所以求f(a[I])必须使用矩阵加速。

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#define LL long long
#define maxn 100005
#define MOD 1000000007
using namespace std;
int exnext[maxn];
void getexnext(char s[])
{
    int len = strlen(s);
    exnext[0] = 0;
    int k, j, i;
    i = j = 0;
    for (k = 1; k < len; k++)
    {
        if (k + exnext[k - i] > j)
        {
            i = k;
            while (j < len&&s[j - i + 1] == s[j + 1]) j++;
            exnext[k] = j - k + 1;
            if (j < k) j = k;
        }
        else exnext[k] = k + exnext[k - i] - 1 < len ? exnext[k - i] : len - k + 1;
    }
}
struct node
{
    int l,r;
    LL add;
} tree[maxn<<2];
void build(int id,int l,int r)
{
    tree[id].l=l;
    tree[id].r=r;
    tree[id].add=0;
    if(l==r) return;
    int mid=(l+r)>>1;
    build(id<<1,l,mid);
    build(id<<1|1,mid+1,r);
}
void op(int id,int l,int r,int add)
{
    if(l<=tree[id].l&&tree[id].r<=r) tree[id].add+=add;
    else
    {
        int mid=(tree[id].l+tree[id].r)>>1;
        if(l<=mid) op(id<<1,l,r,add);
        if(mid<r) op(id<<1|1,l,r,add);
    }
}
void pushdown(int id)
{
    tree[id<<1].add+=tree[id].add;
    tree[id<<1|1].add+=tree[id].add;
}
LL Ans[maxn];
void out(int id)
{
    if(tree[id].l==tree[id].r) Ans[tree[id].l]=tree[id].add;
    else
    {
        pushdown(id);
        out(id<<1);
        out(id<<1|1);
    }
}
struct matrix
{
    LL a[2][2];
};
matrix mul(matrix& x,matrix& y)
{
    matrix ans;
    ans.a[0][0]=x.a[0][0]*y.a[0][0]+x.a[0][1]*y.a[1][0];
    ans.a[0][0]%=MOD;
    ans.a[0][1]=x.a[0][0]*y.a[0][1]+x.a[0][1]*y.a[1][1];
    ans.a[0][1]%=MOD;
    ans.a[1][0]=x.a[1][0]*y.a[0][0]+x.a[1][1]*y.a[1][0];
    ans.a[1][0]%=MOD;
    ans.a[1][1]=x.a[1][0]*y.a[0][1]+x.a[1][1]*y.a[1][1];
    ans.a[1][1]%=MOD;
    return ans;
}
matrix power(matrix x,long e)
{
    matrix ans,tmp;
    if(e==0)
    {
        ans.a[0][0]=1;
        ans.a[0][1]=0;
        ans.a[1][0]=0;
        ans.a[1][1]=1;
        return ans;
    }
    if( e==1 )
        return x;
    tmp=power(x,e>>1);
    ans=mul(tmp,tmp);
    if( e&1 )
        ans=mul(ans,x);
    return ans;
}
LL fic(int n)
{
    matrix result = {{1,0,0,1}};
    matrix base = {{1,1,1,0}};
    if(n&1) result = base;
    n>>=1;
    while(n)
    {
        base=mul(base, base);
        if(n&1)
        {
            result=mul(result, base);
        }
        n>>=1;
    }
    return result.a[0][1];
}
char s[maxn];
int main()
{
    while(~scanf("%s",s))
    {
        getexnext(s);
        int len=strlen(s);
        LL ans=0;
        exnext[0]=len;
        build(1,0,len-1);
        for(int i=0;i<len;i++) op(1,0,i,exnext[i]);
        out(1);
        for(int i=0;i<len;i++) ans=(ans+fic(Ans[i]))%MOD;
        printf("%lld\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值