zoukankan      html  css  js  c++  java
  • codeforces 645E . Intellectual Inquiry

    题目链接

    如果不考虑重复的元素, 那么我们可以很容易的发现, 长度为n的字符串它的子串数量是 $ 2^n $ 。

    我们设每个到位置i, 答案的数量为f[i]。
    然后我们考虑重复的, 我们发现, 每加入一个字符c, 记它出现的上一个位置为last[c], 那么last[c]之前的字符和last[c]产生的字符串与 last[c]之前的字符和c产生的字符串就会发生重复。 所以我们减掉f[last[c]-1]。

    对于长度大于m, 我们要新加入的字符, 肯定是选一个last[c]最小的来加, 这样产生的重复就越小。

    #include <iostream>
    #include <vector>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <map>
    #include <set>
    #include <string>
    #include <queue>
    #include <stack>
    #include <bitset>
    using namespace std;
    #define pb(x) push_back(x)
    #define ll long long
    #define mk(x, y) make_pair(x, y)
    #define lson l, m, rt<<1
    #define mem(a) memset(a, 0, sizeof(a))
    #define rson m+1, r, rt<<1|1
    #define mem1(a) memset(a, -1, sizeof(a))
    #define mem2(a) memset(a, 0x3f, sizeof(a))
    #define rep(i, n, a) for(int i = a; i<n; i++)
    #define fi first
    #define se second
    typedef pair<int, int> pll;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int mod = 1e9+7;
    const int inf = 1061109567;
    const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
    const int maxn = 2e6+5;
    ll f[maxn];
    int last[28];
    char s[maxn/2];
    int main()
    {
        int n, m, k;
        cin>>m>>k;
        scanf("%s", s+1);
        n = strlen(s+1);
        for(int i = 0; i<k; i++)
            last[i] = 0;
        f[0] = 1;
        for(int i = 1; i<=n+m; i++) {
            f[i] = f[i-1]*2%mod;
            int c = -1, pos = i;
            if(i<=n) {
                c = s[i]-'a';
                pos = last[c];
            } else {
                for(int j = 0; j<k; j++) {
                    if(last[j]<pos) {
                        pos = last[j];
                        c = j;
                    }
                }
            }
            f[i] = (f[i]-f[pos-1]+mod)%mod;
            last[c] = i;
        }
        cout<<f[m+n]<<endl;
        return 0;
    }
    
    
  • 相关阅读:
    DC中为什么要用Uniquify?
    hdu 1596 find the safest road
    hdu2112 HDU Today
    hdu 2066 一个人的旅行
    poj 3026 Borg Maze
    poj 1979 Red and Black
    poj 1321 棋盘问题
    hdu 1010 Tempter of the Bone
    hdu 4861 Couple doubi
    codeforces584B Kolya and Tanya
  • 原文地址:https://www.cnblogs.com/yohaha/p/5294573.html
Copyright © 2011-2022 走看看