Good Article Good sentence HDOJ4416 (SAM解法)

这题比poj3415简单,只要记录一下到达每个状态的最大长度就可以了,关键还是用失配指针转移。


#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(100010);
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(2520);
const ULL BASE(31);
const ULL LIM(1000000000000000ull);

inline int idx(char temp)
{
    return temp-'a';
}

char str[MAXN];
int cnt[MAXN];
int buc[MAXN << 1];
int len;

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

    NODE *newnode(int tl)
    {
        pool[size].len = tl;
        pool[size].val = pool[size].mi = 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);
        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;
            }
        }
    }
    SAM::NODE *que[MAXN << 1];
    int front, back;
    void getMin()   //得到每个状态的min
    {
        front = back = 0;
        que[back++] = root;
        SAM::NODE *p;
        while(front < back)
        {
            p = que[front++];
            for(int i = 0; i < SIGMA_SIZE; ++i)
                if(p->ch[i] && !p->ch[i]->mi)
                {
                    p->ch[i]->mi = p->mi+1;
                    que[back++] = p->ch[i];
                }
        }
    }
    void turpo()  //按MAX拓扑
    {
        memset(cnt, 0, sizeof(cnt[0])*(len+1));
        for(int i = 0; i < size; ++i) ++cnt[pool[i].len];
        for(int i = 1; i <= len; ++i) cnt[i] += cnt[i-1];
        for(int i = 0; i < size; ++i) buc[--cnt[pool[i].len]] = i;
    }
/*    void getRight()  //得到每个状态的right
    {
        NODE *tp = root;
        for(char *sp = str; *sp; ++sp)
            tp = tp->ch[idx(*sp)], tp->right = 1;
        for(int i = size-1; i >= 0; --i)
        {
            tp = pool+buc[i];
            if(tp->f)
                tp->f->right += tp->right; 
        }
    }*/
};

SAM sam;


int main()
{
    int TC, n_case(0);
    scanf("%d", &TC);
    while(TC--)
    {
        int n;
        scanf("%d", &n);
        sam.init();
        scanf("%s", str);
        len = strlen(str);
        for(int i = 0; i < len; ++i)
            sam.extend(idx(str[i]));
        sam.turpo();
        sam.getMin();
        SAM::NODE *tp;
        for(int i = 0; i < n; ++i)
        {
            scanf("%s", str);
            tp = sam.root;
            int l = 0;
            for(char *sp = str; *sp; ++sp)
            {
                int id = idx(*sp);
                if(tp->ch[id])
                {
                    ++l;
                    tp = tp->ch[id];
                }
                else
                {
                    while(tp && tp->ch[id] == 0)
                        tp = tp->f;
                    if(tp)
                    {
                        l = tp->len+1;
                        tp = tp->ch[id];
                    }
                    else
                    {
                        tp = sam.root;
                        l = 0;
                    }
                }
                tp->val = max(tp->val, l);
            }
        }
        LL ans = 0;
        for(int i = sam.size-1; i > 0; --i)
        {
            tp = sam.pool+buc[i];
            ans += tp->len-max(tp->mi-1, tp->val);
            if(tp->f && tp->val)
                tp->f->val = tp->f->len;
        }
        printf("Case %d: %I64d\n", ++n_case, ans);
    }
    return 0;
}


你可能感兴趣的:(Good Article Good sentence HDOJ4416 (SAM解法))