HDU 4270 Dynamic Lover(12年长春网络赛 SAM)

来源:互联网 发布:bootstrap 下拉菜单js 编辑:程序博客网 时间:2024/06/09 19:59

转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove

题目:给出一个串,有3种操作,
操作1:在原串后面,添加一个串
操作2:查询长度为len的子串或者长度小于等于len的后缀中字典序最小的
操作3:删除最后的len个字符
http://acm.hdu.edu.cn/showproblem.php?pid=4270 
debug到死啊,各种TLE,各种WA。哭瞎了都~~~~~~~
初始想法是没问题的:
对于操作1:直接在后面add字符进SAM就行了
对于操作2:查询字典序最小的话,直接DFS吧。由于还可以是长度不定的后缀,所以把所有的终态标记一下。
                    从tail出发,沿pre指针,给个特定的指针,表示本次查询的终态。
                    字典序最小,从小到大,遍历,肯定是最小的,至少会遇到一个长度 为len的子串,或者遇到终态。
对于操作3:是最纠结的地方,也是换了好几种姿势的地方。
                   版本一:用pos[i]表示字符长度为i的时候的结点编号,开始觉得从pos[l-len+1]……tot都是应该删除的结点。首先加入一个字符,必然会添加一个结点,也可以再构造出一个结点,但是需要注意的是这个结点nq,不是和np等价的,还是从q中复制出来的。
                  版本二:从pos[l-len+1]……tot把这个结点在串的位置id>=l-len+1的结点删除,也就是之后添加的结点,感 觉是对的啊?常数很大?不会啊,可是就是TLE
                 版本三:直接用vector记录串中每个位置复制出来的结点的编号,遍历长度然后遍历这些结点,删除,还是TLE
                 版本四:无奈看了别人的做法,在SAM建立一个指针*del,表示是否需要删除,而且所以复制出来的共用   一个指针,那么只需要修改一次,就可以把所有的结点删除,ORZ
其它地方注意细节就好了,指针真心不好调试,sad,不知道其它人的做法是怎么处理删除的
#include<iostream>  #include<cstdio>  #include<map>  #include<cstring>  #include<cmath>  #include<vector>  #include<algorithm>  #include<set>  #include<string>  #include<queue>  #define inf 1600005  #define M 40  #define N 510001#define maxn 2000005  #define eps 1e-7#define zero(a) fabs(a)<eps  #define Min(a,b) ((a)<(b)?(a):(b))  #define Max(a,b) ((a)>(b)?(a):(b))  #define pb(a) push_back(a)  #define mp(a,b) make_pair(a,b)  #define mem(a,b) memset(a,b,sizeof(a))  #define LL long long  #define MOD 1000000007#define lson step<<1#define rson step<<1|1#define sqr(a) ((a)*(a))  #define Key_value ch[ch[root][1]][0]  #define test puts("OK");  #define pi acos(-1.0)#define lowbit(x) ((x)&(-(x)))#pragma comment(linker, "/STACK:1024000000,1024000000")  #define vi vector<int> using namespace std;  struct SAM{    SAM *pre,*son[26];    int len,id,suf;    bool *del;    void Init(){        pre=NULL;        id=len=suf=0;        mem(son,NULL);    }}*root,*tail,que[N];int tot,D;char str[N/2];int q,l;int pos[N/2];bool del[N/2]={false};void add(int c,int l){    SAM *np=&que[tot++],*p=tail;    np->Init();    np->del=&del[D++];    *np->del=false;    pos[l]=tot-1;    np->len=np->id=l;    while(p&&(p->son[c]==NULL||(*p->son[c]->del))) p->son[c]=np,p=p->pre;    if(p==NULL) np->pre=root;    else{        SAM *q=p->son[c];        if(p->len+1==q->len)np->pre=q;        else{            SAM *nq=&que[tot++];               *nq=*q;            nq->len=p->len+1;            np->pre=q->pre=nq;            while(p&&(p->son[c]==q)) p->son[c]=nq,p=p->pre;        }    }    tail=np;}void Delete(int len){    for(int i=l-len+1;i<=l;i++) *(que[pos[i]].del)=true;    l-=len;    tail=&que[pos[l]];}int cnt,len,k;int dfs(SAM *p,int idx){    if(idx==len)        return p->id-idx+1;    if(p->suf==cnt)        return l-idx+1;    for(int i=0;i<26;i++)        if(p->son[i]!=NULL&&!(*p->son[i]->del))            return dfs(p->son[i],idx+1);}int main(){    //freopen("input.txt","r",stdin);    //freopen("output.txt","w",stdout);    while(scanf("%s",str)!=EOF){        tot=0;l=0;cnt=0;D=0;        root=tail=&que[tot++];        root->Init();        root->del=&del[D++];*root->del=false;        scanf("%d",&q);        for(int i=0;str[i];i++) add(str[i]-'a',++l);        while(q--){            scanf("%d",&k);            if(k==1){                scanf("%s",str);                for(int i=0;str[i];i++){                    add(str[i]-'a',++l);                }            }            else{                scanf("%d",&len);                if(k==3) Delete(len);                else{                    cnt++;                    SAM *p=tail;                    while(p!=NULL&&p!=root) p->suf=cnt,p=p->pre;                    root->suf=0;                    printf("%d\n",dfs(root,0));                }            }        }    }    return 0;}


原创粉丝点击