Cyclical Quest CF235C

开始在处理失配指针传递的时候卡住了,以为如果每次都按topo序从后往前递推的话复杂度太高,看了一下别人的代码发现只需要把遍历到的节点向前递归即可,而且要注意每个节点只能累加一次,用个时间戳处理一下就可以了。




#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::istringstream;
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(1000010);
const int SIGMA_SIZE(26);
const int MAXM(110);
const int MAXE(300010);
const int MAXH(18);
const int INFI((INT_MAX-1) >> 1);
const int MOD(2012);
const ULL BASE(31);
const ULL LIM(1000000000000000ull);

int cnt[MAXN];
int buc[MAXN << 1];
int idx(char temp)
{
    return temp-'a';
}

struct SAM
{
    struct NODE
    {
        int len;
        int stamp, count;
        NODE *f, *ch[SIGMA_SIZE];   
    };

    NODE pool[MAXN << 1];
    NODE *root, *last;
    int size;

    void init()
    {
        root = last = pool;
        root->f = 0;
        root->len = 0;
        root->count = root->stamp = 0;
        memset(root->ch, 0, sizeof(root->ch));
        size = 1;
    }

    NODE *newnode(int tl)
    {
        pool[size].len = tl;
        pool[size].count = pool[size].stamp = 0;
        memset(pool[size].ch, 0, sizeof(pool[size].ch));
        return pool+size++;
    }

    void extend(int id)
    {
        NODE *p = last, *np = newnode(last->len+1);
        np->count = 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;
                np->f = q->f = nq;
                while(p && p->ch[id] == q)
                    p->ch[id] = nq, p = p->f;
            }
        }
    }

    void topo()
    {
        int mlen = last->len;
        memset(cnt, 0, sizeof(cnt[0])*(mlen+1));
        for(int i = 0; i < size; ++i) ++cnt[pool[i].len];
        for(int i = 1; i <= mlen; ++i) cnt[i] += cnt[i-1];
        for(int i = 0; i < size; ++i) buc[--cnt[pool[i].len]] = i;
    }
/*
    void getFront()
    {
        for(int i = 0; i < size; ++i)
        {
            NODE *p = pool+buc[i];
            int temp = p->mi+1;
            for(int j = 0; j < SIGMA_SIZE; ++j)
                if(p->ch[j])
                    p->ch[j]->mi = min(p->ch[j]->mi, temp);
        }
    }
*/
    void getBack()
    {
        for(int i = size-1; i > 0; --i)
        {
            NODE *p = pool+buc[i];
            p->f->count += p->count;
        }
    }
};

SAM sam;
char str[MAXN << 1];

int main()
{
    while(~scanf("%s", str))
    {
        sam.init();
        for(char *sp = str; *sp; ++sp)
            sam.extend(idx(*sp));
        sam.topo();
        sam.getBack();
        
        int n;
        scanf("%d", &n);
        for(int i = 1; i <= n; ++i)
        {
            scanf("%s", str);
            int len = strlen(str);
            memcpy(str+len, str, sizeof(str[0])*len);
            int tlen = len*2;
            str[tlen] = '\0';
            SAM::NODE *p = sam.root;
            int l = 0, ans = 0;
            for(char *sp = str; *sp; ++sp)
            {
                int id = idx(*sp);
                if(p->ch[id])
                {
                    p = p->ch[id];
                    ++l;
                }
                else
                {
                    while(p && p->ch[id] == 0) p = p->f;
                    
                    if(p)
                    {
                        l = p->len+1;
                        p = p->ch[id];
                    }
                    else
                    {
                        l = 0;
                        p = sam.root;
                    }
                }
                if(l >= len)
                {
                    SAM::NODE *tp = p;
                    while(tp->f && tp->f->len >= len) tp = tp->f;
                    if(tp->stamp != i)
                    {
                        ans += tp->count;
                        tp->stamp = i;
                    }
                }
            }
            printf("%d\n", ans);
        }
    }
    return 0;
}


你可能感兴趣的:(Cyclical Quest CF235C)