Good Substrings CF271D

SAM上的DP


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>   
#include <map>
#include <string>  
#include <climits> 
#include <set>
#include <string>    
#include <sstream>
#include <utility>   
#include <ctime>

using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::stringstream;
using std::make_pair;
using std::getline;
using std::greater;
using std::endl;
using std::multimap;
using std::deque;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PAIR;
typedef multimap<int, int> MMAP;

const int MAXN(1510);
const int MAXM(5010);
const int MAXE(10010);
const int HSIZE(13131);
const int SIGMA_SIZE(26);
const int MAXH(19);
const int INFI((INT_MAX-1) >> 1);
const ULL BASE(31);
const LL LIM(10000000);
const int INV(-10000);

int K;
int score[26];

struct SAM
{
    struct NODE
    {
        int len;
        NODE *f, *ch[SIGMA_SIZE];
        int table[MAXN];
    };
    NODE *root, *last;
    NODE pool[MAXN << 1];
    int size;
    void init()
    {
        root = last = pool;
        root->f = 0;
        root->len = 0;
        memset(root->ch, 0, sizeof(root->ch));
        memset(root->table, -1, sizeof(root->table[0])*(K+1));
        size = 1;
    }

    NODE *newnode(int tl)
    {
        pool[size].len = tl;
        memset(pool[size].ch, 0, sizeof(pool[size].ch));
        memset(pool[size].table, -1, sizeof(pool[size].table[0])*(K+1));
        return pool+size++;
    }
    void extend(int id)
    {
        NODE *p = last, *np = newnode(last->len+1);
        last = np;
        while(p && p->ch[id] == 0)
            p->ch[id] = np, p = p->f;
        if(p == 0)
            np->f = root;
        else
        {
            NODE *q = p->ch[id];
            if(p->len+1 == q->len)
                np->f = q;
            else
            {
                NODE *nq = newnode(p->len+1);
                memcpy(nq->ch, q->ch, sizeof(nq->ch));
                nq->f = q->f;
                q->f = np->f = nq;
                while(p && p->ch[id] == q)
                    p->ch[id] = nq, p = p->f;
            }
        }
    }
    int dfs(NODE *p, int cnt)
    {
        if(cnt > K)
            return 0;
        if(p->table[cnt] != -1)
            return p->table[cnt];
        int &cur = p->table[cnt];
        cur = 1;
        for(int i = 0; i < SIGMA_SIZE; ++i)
            if(p->ch[i])
                cur += dfs(p->ch[i], cnt+score[i]);
        return cur;
    }
};

SAM sam;
char str[MAXN];

int main()
{
    while(~scanf("%s", str))
    {
        sam.init();
        char temp;
        for(int i = 0; i < 26; ++i)
        {
            scanf(" %c", &temp);
            score[i] = (temp-'0')^1;
        }
        scanf("%d", &K);
        for(char *sp = str; *sp; ++sp)
            sam.extend(*sp-'a');
        sam.dfs(sam.root, 0);
        printf("%d\n", sam.root->table[0]-1);
    }
    return 0;
}


D. Good Substrings time limit per test2 seconds memory limit per test512 megabytes You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad. A substring s[l...r] (1 ≤ l ≤ r ≤ |s|) of string s  =  s1s2...s|s| (where |s| is the length of string s) is string  slsl + 1...sr. The substring s[l...r] is good, if among the letters  sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear). Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] ≠ s[p...q]. Input The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters. The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on. The third line of the input consists a single integer k (0 ≤ k ≤ |s|) — the maximum acceptable number of bad characters in a good substring. Output Print a single integer — the number of distinct good substrings of string s. Examples InputCopy ababab 01000000000000000000000000 1 OutputCopy 5 InputCopy acbacbacaa 00000000000000000000000000 2 OutputCopy 8 Note In the first example there are following good substrings: "a", "ab", "b", "ba", "bab". In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
最新发布
03-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值