zoukankan      html  css  js  c++  java
  • Codeforces 356D Bacterial Melee dp

    Bacterial Melee

    我们发现所有合法串都是原序列的某个子序列(这个子序列相邻元素相等) 的扩展, 比如子序列为abc, 那么aabbbc, abbbcc 等都是合法串。

    所以我们只需要dp出原串有多少相邻元素不同的子序列就好啦。

    #include<bits/stdc++.h>
    #define LL 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 ull unsigned long long
    using namespace std;
    
    const int N = 5000 + 7;
    const int inf = 0x3f3f3f3f;
    const LL INF = 0x3f3f3f3f3f3f3f3f;
    const int mod = 1e9 + 7;
    const double eps = 1e-8;
    
    int n, f[N][26], g[N][26];
    int comb[N][N];
    char s[N];
    bool vis[26];
    
    inline void add(int &a, int b) {
        a += b; if(a >= mod) a -= mod;
    }
    
    int main() {
        for(int i = 0; i < N; i++)
            for(int j = comb[i][0] = 1; j <= i; j++)
                comb[i][j] = (comb[i - 1][j - 1] + comb[i - 1][j]) % mod;
        scanf("%d%s", &n, s + 1);
        f[1][s[1] - 'a'] = 1; vis[s[1] - 'a'] = true;
        for(int i = 2; i <= n; i++) {
            memcpy(g, f, sizeof(g));
            for(int j = i - 1; j >= 1; j--) {
                for(int k = 0; k < 26; k++) {
                    if(k != s[i] - 'a') add(f[j + 1][s[i] - 'a'], g[j][k]);
                }
                add(f[j + 1][s[i] - 'a'], mod - g[j + 1][s[i] - 'a']);
            }
            if(!vis[s[i] - 'a']) {
                vis[s[i] - 'a'] = true;
                f[1][s[i] - 'a'] = 1;
            }
        }
        int ans = 0;
        for(int i = 1; i <= n; i++)
            for(int j = 0; j < 26; j++)
                add(ans, 1ll * comb[n - 1][i - 1] * f[i][j] % mod);
        printf("%d
    ", ans);
        return 0;
    }
    
    /*
    */
  • 相关阅读:
    mina之小小总结(标准的菜鸟级别,行家勿入)
    tomcat(就一句话,自己的日记性质)
    MINA转自itoyo
    java web(没含量的,百科上的)
    解决VS.NET 2008中aspx文件没有设计界面
    正则表达式教程
    [VB] Option Explicit
    sql自定義函數 包含遊標
    sql 中 null+others=?
    实用手机号、IP、身份证号、歌曲查询接口
  • 原文地址:https://www.cnblogs.com/CJLHY/p/10508250.html
Copyright © 2011-2022 走看看