zoukankan      html  css  js  c++  java
  • Codeforces 696D Legen... AC自动机 + 矩阵快速幂

    Legen...

    我们能很容易写出在AC自动机上的dp, dp[ k ][ i ][ j ]表示走了 k 步从 i 走到 j 的最大值。

    k 很大我们考虑矩阵优化, 直接搞就好啦。

    不知道为什么在本机上M * M * M * M **** * M 打多了就会卡死不知道为什么。。。

    #include<bits/stdc++.h>
    #define LL long long
    #define LD long double
    #define ull unsigned long long
    #define fi first
    #define se second
    #define mk make_pair
    #define PLL pair<LL, LL>
    #define PLI pair<LL, int>
    #define PII pair<int, int>
    #define SZ(x) ((int)x.size())
    #define ALL(x) (x).begin(), (x).end()
    #define fio ios::sync_with_stdio(false); cin.tie(0);
    
    using namespace std;
    
    const int N = 200 + 7;
    const int inf = 0x3f3f3f3f;
    const LL INF = 0x3f3f3f3f3f3f3f3f;
    const int mod = 998244353;
    const double eps = 1e-8;
    const double PI = acos(-1);
    
    template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
    template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;}
    template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
    template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}
    
    int n, a[N];
    LL l;
    char s[N][N];
    
    int MN;
    struct Matrix {
        LL a[207][207];
        Matrix() {
            for(int i = 0; i < MN; i++)
                for(int j = 0; j < MN; j++)
                    a[i][j] = 0;
        }
        void init() {
            for(int i = 0; i < MN; i++)
                for(int j = 0; j < MN; j++)
                    a[i][j] = -INF;
        }
        Matrix operator * (const Matrix &B) const {
            Matrix C; C.init();
            for(int i = 0; i < MN; i++)
                for(int j = 0; j < MN; j++)
                    for(int k = 0; k < MN; k++)
                        chkmax(C.a[i][j], a[i][k] + B.a[k][j]);
            return C;
        }
        Matrix operator ^ (LL b) {
            Matrix C = (*this);
            Matrix A = (*this);
            b--;
            while(b) {
                if(b & 1) C = C * A;
                A = A * A; b >>= 1;
            }
            return C;
        }
    } M;
    
    
    struct Ac { // 1. init before use  2. cheke character set
        int ch[N][26], f[N], tot, sz, subVal;
        int val[N];
        inline int newNode() {
            tot++; f[tot] = 0;
            memset(ch[tot], 0, sizeof(ch[tot]));
            return tot;
        }
        void init(int _sz, int _subVal) {
            sz = _sz; subVal = _subVal;
            tot = -1; newNode();
        }
        inline int idx(int c) {return c - subVal;}
        void addStr(char* s, int w) {
            int u = 0;
            for(int i = 0; s[i]; i++) {
                int c = idx(s[i]);
                if(!ch[u][c]) ch[u][c] = newNode();
                u = ch[u][c];
            }
            val[u] += w;
        }
        void build() {
            queue<int> que;
            for(int c = 0; c < sz; c++) {
                int v = ch[0][c];
                if(!v) ch[0][c] = 0;
                else f[v] = 0, que.push(v);
            }
            while(!que.empty()) {
                int u = que.front(); que.pop();
                val[u] += val[f[u]];
                for(int c = 0; c < sz; c++) {
                    int v = ch[u][c];
                    if(!v) ch[u][c] = ch[f[u]][c];
                    else f[v] = ch[f[u]][c], que.push(v);
                }
            }
        }
        void getMatrix() {
            MN = tot + 1;
            for(int i = 0; i < MN; i++)
                for(int j = 0; j < MN; j++)
                    M.a[i][j] = -INF;
            for(int u = 0; u <= tot; u++) {
                for(int c = 0; c < sz; c++) {
                    int v = ch[u][c];
                    if(!v) continue;
                    chkmax(M.a[u][v], val[v]);
                }
            }
        }
    } ac;
    
    int main() {
        ac.init(26, 'a');
        scanf("%d%lld", &n, &l);
        for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
        for(int i = 1; i <= n; i++) scanf("%s", s[i]);
        for(int i = 1; i <= n; i++) ac.addStr(s[i], a[i]);
        ac.build();
        ac.getMatrix();
    
        M = M ^ l;
    
        LL ans = 0;
        for(int i = 0; i < MN; i++)
            chkmax(ans, M.a[0][i]);
        printf("%lld
    ", ans);
        return 0;
    }
    
    /*
    */
  • 相关阅读:
    (转载)Android mac 真机调试
    Google Map SDK for iOS
    autolayout under navigation bar
    Xamarin Mono For Android、Monotouch 安装
    不错的ORM调试查看SQL插件
    在Windows 8.1系统上配置免安装版mysql-5.6.21-winx64
    VS2013 EF6连接MySql
    安卓离线SDK Windows版 资源包下载地址全集
    WndProc函数参数列表
    反射创建对象
  • 原文地址:https://www.cnblogs.com/CJLHY/p/10957954.html
Copyright © 2011-2022 走看看