Good Substrings

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

SAM上DP

在某岛的博客上看到的,要义就是一个串的子串必然是该串某个后缀的前缀,所以从表示该状态的子串节点往后走必然会走到那个串的尾节点。




#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include    
#include 
#include   
#include  
#include 
#include     
#include 
#include    
#include 
#include 
#include 
//#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 PAIR;
typedef multimap 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 inline void checkmax(T &a, T b){if(b > a) a = b;}
template inline void checkmin(T &a, T b){if(b < a) a = b;}
template 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;
}


你可能感兴趣的:(动态规划,串,数据结构)