Good Substrings

78 篇文章 2 订阅
55 篇文章 0 订阅

http://codeforces.com/contest/316/problem/G3

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>
#include <bitset>
#include <iomanip>
//#pragma comment(linker, "/STACK:102400000,102400000")

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;
using std::unique;
using std::lower_bound;
using std::random_shuffle;
using std::bitset;
using std::upper_bound;
using std::multiset;
using std::ios;
using std::make_heap;
using std::push_heap;
using std::pop_heap;

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

const int MAXN(600100);
const int MAXM(310);
const int MAXE(310);
const int MAXK(6);
const int HSIZE(13131);
const int SIGMA_SIZE(26+11);
const int MAXH(18);
const int INFI((INT_MAX-1) >> 1);
const ULL BASE(31);
const LL LIM(1e13);
const int INV(-10000);
const int MOD(1000000007);
const double EPS(1e-7);
const LF PI(acos(-1.0));

template<typename T> inline void checkmax(T &a, T b){if(b > a) a = b;}
template<typename T> inline void checkmin(T &a, T b){if(b < a) a = b;}
template<typename T> inline T ABS(const T &a){return a < 0? -a: a;}

struct NODE
{
    int len;
    NODE *ch[SIGMA_SIZE], *f;
    int dp[11];
};

struct SAM
{
    NODE pool[MAXN << 1], *rear, *last;
    void init()
    {
        last = pool;
        last->len = 0;
        last->f = 0;
        memset(last->ch, 0, sizeof(last->ch));
        rear = pool+1;
    }
    void extend(int id)
    {
        NODE *p = last, *np = rear++;
        np->len = last->len+1;
        memset(np->ch, 0, sizeof(np->ch));
        last = np;
        while(p && !p->ch[id])
        {
            p->ch[id] = np;
            p = p->f;
        }
        if(!p) np->f = pool;
        else
        {
            NODE *q = p->ch[id];
            if(q->len == p->len+1) np->f = q;
            else
            {
                NODE *nq = rear++;
                nq->len = 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 buc[MAXN], size;
    NODE *rec[MAXN << 1];
    void tupo()
    {
        size = rear-pool;
        memset(buc, 0, sizeof(buc[0])*(last->len+1));
        for(NODE *p = pool; p < rear; ++p) ++buc[p->len];
        for(int i = 1; i <= last->len; ++i) buc[i] += buc[i-1];
        for(NODE *p = rear-1; p >= pool; --p) rec[--buc[p->len]] = p;
    }
} sam;

char str[50010];
int l[11], r[11];

int main()
{
    scanf("%s", str);
    sam.init();
    for(char *sp = str; *sp; ++sp) sam.extend(*sp-'a');
    sam.extend(26);
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i)
    {
        scanf("%s%d%d", str, l+i, r+i);
        for(char *sp = str; *sp; ++sp) sam.extend(*sp-'a');
        sam.extend(26+i);
    }
    sam.tupo();
    int ans = 0;
    for(int i = sam.size-1; i > 0; --i)
    {
        NODE *p = sam.rec[i];
        for(int i = 0; i <= n; ++i)
            if(p->ch[i+26])
                p->dp[i] = 1;
        for(int i = 0; i < 26; ++i)
            if(p->ch[i])
            {
                for(int j = 0; j <= n; ++j) p->dp[j] += p->ch[i]->dp[j];
            }
        if(p->dp[0])
        {
            bool flag(true);
            for(int i = 1; i <= n; ++i)
                if(p->dp[i] < l[i] || p->dp[i] > r[i])
                {
                    flag = false;
                    break;
                }
            if(flag) ans += p->len-p->f->len;
        }
    }
    printf("%d\n", ans);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值