zoukankan      html  css  js  c++  java
  • luogu P5337 [TJOI2019]甲苯先生的字符串

    传送门

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

    首先可以写出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
    ",ans);
        return 0;
    }
    
  • 相关阅读:
    用OpenGL简单编写的一个最简单贪吃蛇游戏
    Python lambda map filter reduce
    Hadoop Python MapReduce
    Python faker生成数据
    Pandas数据清洗
    PySpark与jupyer notebook
    虚拟机与宿主机网络共享
    集合覆盖问题与贪婪算法
    最快路径与狄克斯特拉
    最短路径问题与广度优先搜索
  • 原文地址:https://www.cnblogs.com/smyjr/p/10841142.html
Copyright © 2011-2022 走看看