Dynamic Lover HDU4270

继续SAM,删除时仿照爱神的思路搞的,注意在利用失配指针传递串结尾时要使用时间戳,否则每次都清空flag的话会TLE。


#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(200010);
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);

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

struct SAM
{
    struct NODE
    {
        int len;
        int flag, ind;
        bool *del;
        NODE *ch[SIGMA_SIZE], *f;
    };
    NODE pool[MAXN << 1];
    NODE *pos[MAXN];
    int size;
    NODE *root, *last;
    void init()
    {
        root = last = pool;
        root->len = 0;
        root->f = 0;
        root->ind = 0;
        root->flag = 0;
        root->del = delp+dels++;
        (*root->del) = false;
        pos[0] = root;
        memset(root->ch, 0, sizeof(root->ch));
        size = 1;
    }
    NODE *newnode(int tl)
    {
        pool[size].len = tl;
        pool[size].flag = 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);
        pos[np->len] = np;
        np->del = delp+dels++;
        *(np->del) = false;
        np->ind = np->len;
        last = np;
        while(p && (p->ch[id] == 0 || *(p->ch[id]->del)))
            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->ind = q->ind;
                nq->del = q->del;
                nq->f = q->f;
                q->f = np->f = nq;
                while(p && p->ch[id] == q)
                    p->ch[id] = nq, p = p->f;
            }
        }
    }
/*
    void turpo()
    {
        memset(cnt, 0, sizeof(cnt[0])*(last->len+1));
        for(int i = 0; i < size; ++i) ++cnt[pool[i].len];
        for(int i = 1; i <= last->len; ++i) cnt[i] += cnt[i-1];
        for(int i = 0; i < size; ++i) buc[--cnt[pool[i].len]] = i;
    }
*/
    void fun(int ind)
    {
        NODE *p = last;
        while(p != root)
            p->flag = ind, p = p->f;
    }
    
    int solve(int ind, int tl)
    {
        fun(ind);
        NODE *p = root;
        int i;
        for(i = 0; i < tl; ++i)
        {
            if(p->flag == ind)
                break;
            for(int j = 0; j < SIGMA_SIZE; ++j)
                if(p->ch[j] && !*(p->ch[j]->del))
                {
                    p = p->ch[j];
                    break;
                }
        }
        return i == tl? p->ind-tl: last->ind-i;
    }

    void Del(int tl)
    {
        int nl = last->len;
        for(int i = 0; i < tl; ++i)
            *(pos[nl-i]->del) = true;
        last = pos[nl-tl];
    }
};


SAM sam;

char str[MAXN];

int main()
{
    while(~scanf("%s", str))
    {
        dels = 0;
        sam.init();
        for(char *sp = str; *sp; ++sp)
            sam.extend(idx(*sp));
        int n;
        scanf("%d", &n);
        int op, tl;
        for(int i = 0; i < n; ++i)
        {
            scanf("%d", &op);
            switch(op)
            {
            case 1:        scanf("%s", str);
                        for(char *sp = str; *sp; ++sp)
                            sam.extend(idx(*sp));
                        break;
            case 2:        scanf("%d", &tl);
                        printf("%d\n", sam.solve(i+1, tl)+1);
                        break;
            case 3:        scanf("%d", &tl);
                        if(tl == sam.last->len)
                        {
                            dels = 0;
                            sam.init();
                        }
                        else
                            sam.Del(tl);
                        break;
            }
        }
    }
    return 0;
}






你可能感兴趣的:(Dynamic Lover HDU4270)