码迷,mamicode.com
首页 > 其他好文 > 详细

luogu P5337 [TJOI2019]甲苯先生的字符串

时间:2019-05-09 21:59:58      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:math   etc   关系   operator   print   define   org   eof   字符串   

传送门

所以这题和字符串有什么关系

首先可以写出dp,\(f_{i,j}\)表示前\(i\)位,最后一个字符是\(j\)的方案,转移枚举下一位,只要不在大串中前后相邻即可.然后矩乘优化即可

// luogu-judger-enable-o2
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<cmath>
#include<ctime>
#include<queue>
#include<map>
#include<set>
#define LL long long
#define db double

using namespace std;
const int N=1e5+10,mod=1e9+7;
LL rd()
{
    LL x=0,w=1;char ch=0;
    while(ch<'0'||ch>'9'){if(ch=='-') w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
    return x*w;
}
struct matrix
{
    int a[26][26];
    matrix(){memset(a,0,sizeof(a));}
    matrix operator * (const matrix &bb) const
    {
        matrix an;
        for(int i=0;i<26;++i)
            for(int j=0;j<26;++j)
            {
                LL nw=0;
                for(int k=0;k<26;++k) nw+=1ll*a[i][k]*bb.a[k][j]%mod;
                an.a[i][j]=nw%mod;
            }
        return an;
    }
    matrix operator ^ (const LL &bb) const
    {
        matrix an,a=*this;
        for(int i=0;i<26;++i) an.a[i][i]=1;
        LL b=bb;
        while(b)
        {
            if(b&1) an=an*a;
            a=a*a,b>>=1;
        }
        return an;
    }
}aa,bb;
LL n,m;
char cc[N];

int main()
{
    for(int i=0;i<26;++i)
    {
        aa.a[0][i]=1;
        for(int j=0;j<26;++j) bb.a[i][j]=1;
    }
    n=rd();
    scanf("%s",cc+1);
    m=strlen(cc+1);
    for(int i=2;i<=m;++i) bb.a[cc[i-1]-'a'][cc[i]-'a']=0;
    aa=aa*(bb^(n-1));
    int ans=0;
    for(int i=0;i<26;++i) ans=(ans+aa.a[0][i])%mod;
    printf("%d\n",ans);
    return 0;
}

luogu P5337 [TJOI2019]甲苯先生的字符串

标签:math   etc   关系   operator   print   define   org   eof   字符串   

原文地址:https://www.cnblogs.com/smyjr/p/10841142.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!