HDU - 4117 GRE Words (AC自动机+Fail树+线段树+DFS序+DP)

GRE Words


Problem Description
Recently George is preparing for the Graduate Record Examinations (GRE for short). Obviously the most important thing is reciting the words.
Now George is working on a word list containing N words.
He has so poor a memory that it is too hard for him to remember all of the words on the list. But he does find a way to help him to remember. He finds that if a sequence of words has a property that for all pairs of neighboring words, the previous one is a substring of the next one, then the sequence of words is easy to remember.
So he decides to eliminate some words from the word list first to make the list easier for him. Meantime, he doesn't want to miss the important words. He gives each word an importance, which is represented by an integer ranging from -1000 to 1000, then he wants to know which words to eliminate to maximize the sum of the importance of remaining words. Negative importance just means that George thought it useless and is a waste of time to recite the word.
Note that although he can eliminate any number of words from the word list, he can never change the order between words. In another word, the order of words appeared on the word list is consistent with the order in the input. In addition, a word may have different meanings, so it can appear on the list more than once, and it may have different importance in each occurrence.
HDU - 4117 GRE Words (AC自动机+Fail树+线段树+DFS序+DP)_第1张图片
 

Input
The first line contains an integer T(1 <= T <= 50), indicating the number of test cases.
Each test case contains several lines.
The first line contains an integer N(1 <= N <= 2 * 10 4), indicating the number of words.
Then N lines follows, each contains a string S i and an integer W i, representing the word and its importance. S i contains only lowercase letters.
You can assume that the total length of all words will not exceeded 3 * 10 5.
 

Output
For each test case in the input, print one line: "Case #X: Y", where X is the test case number (starting with 1) and Y is the largest importance of the remaining sequence of words.
 

Sample Input
 
   
1 5 a 1 ab 2 abb 3 baba 5 abbab 8
 

Sample Output
 
   
Case #1: 14
 


题意:给你连续的很多串字符串,要你删掉某些字符串,使得剩下的所有字符串,前一个都是后一个的子串,并且剩下的字符串的权值和要最大。


解题思路:做完这道题,我是崩溃的!现在真的真的真的知道Fail指针是什么了……其实这道题还是很好推的DP,用到了简单的DP思想。整道题都能自己推出来,但是打完代码……疯狂MLT,醉了……优化到了极限,连懒惰标记都省了,还是MLT……神奇的是,百度到了一份理论上比我用多超过内存的代码,却刚好卡过了,我百思不得其解……就当自己做出来了好了……


思路就是先把所有字符串建一棵trie树,然后连上fail指针变成AC自动机。然后画个图,很容易想到,fail指针连得越长,答案应该会更大,所以这里要dp,既然要求长的,所以把fail指针反向,建fail树,建出来后,我们就可以一个一个字符串的插进去,并维护一个最大值,这里不能每次都更新整棵fail树的值,所以可以用线段树维护,维护树上的线段树要用dfs,所以要在fail树上求个dfs序。然后就很暴力了…………


我的代码超内存代码……

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long int ll;
#define MAXN 300005
#define  NEXTNUM 26
#define  FC 'a'
#define  INF 0x3f3f3f3f

int nxt[MAXN][NEXTNUM];//trie树
int fail[MAXN];//fail指针
int cnt;

vector Edge[MAXN];//fail树
void insert_edge(int v1,int v2){
    Edge[v1].push_back(v2);
}

//dfs序
int ln[MAXN];
int rn[MAXN];
int SEG;
void dfs(int s){
    ln[s]=++SEG;
    for(int i=0;i que;
    que.push(0);
    while(!que.empty()){
        temp=que.front();
        que.pop();
        if(temp)
            insert_edge(fail[temp],temp);
        for(int i=0;i>1;
    if(R<=m)
        update(L,R,C,l,m,rt<<1);
    else
        if(L>m)
            update(L,R,C,m+1,r,rt<<1|1);
        else{
            update(L,m,C,l,m,rt<<1);
            update(m+1,R,C,m+1,r,rt<<1|1);
        }
}

int query(int L,int l,int r,int rt){
    if(L==l&&r==L)
        return tree[rt];
    int m=(l+r)>>1;

    //直接下推,节省内存
    tree[rt<<1]=max(tree[rt<<1],tree[rt]);
    tree[rt<<1|1]=max(tree[rt<<1|1],tree[rt]);

    int ANS=0;
    if(L<=m)
        ANS=max(ANS,query(L,l,m,rt<<1));
    else
        ANS=max(ANS,query(L,m+1,r,rt<<1|1));
    return ANS;
}

char b[MAXN];
int pos[20005];//保存偏移量,为了省内存……
int val[20005];//权重

int main(){

    int t;
    scanf("%d",&t);
    int N;
    for(int qqq=1;qqq<=t;qqq++){
        scanf("%d",&N);
        SEG=0;
        cnt=0;
        pos[0]=0;
        newnode();
        for(int i=0;itree[1])
                    update(ln[p],rn[p],tmp,1,SEG,1);
                pre=tmp;
            }
        }
        printf("Case #%d: %d\n",qqq,ans);
    }
    return 0;
}



大神的AC代码!看数组的个数……比我多好几倍啊……为什么就AC了?求解释…………

#include 
#include 
#define max(a,b) a>b?a:b
const int mn=300005;
struct Trie{
	int father,fail,next[26];
	void init(){
		father=fail=0;
		memset(next,0,sizeof next);
	}
}tree[mn];
struct Xls{
	int size,y[mn],nxt[mn],fst[mn];
	void set(){
		size=0;
		memset(fst,0,sizeof fst);
	}
	void add(int a,int b){
		y[++size]=b;
		nxt[size]=fst[a];
		fst[a]=size;
	}
}g;
struct Segtree{
	int val,add,l,r;
}segtree[mn*4];
char str[mn];
int pos[mn],n,cnt,val[21000],in[mn],out[mn];
void build(char ch[]){
	int rt=1,t,len=strlen(ch);
	for (int i=0;i>1);
	buildseg(rt<<1|1,((le+ri)>>1)+1,ri);
}
void pushdown(int rt){
	if (segtree[rt].add){
		segtree[rt<<1].add=max(segtree[rt<<1].add,segtree[rt].add);
		segtree[rt<<1].val=max(segtree[rt<<1].val,segtree[rt].add);
		segtree[rt<<1|1].add=max(segtree[rt<<1|1].add,segtree[rt].add);
		segtree[rt<<1|1].val=max(segtree[rt<<1|1].val,segtree[rt].add);
		segtree[rt].add=0;
	}
}
int query(int rt,int x){
	if (segtree[rt].l==segtree[rt].r)
		return segtree[rt].val;
	pushdown(rt);
	if (x<=segtree[rt<<1].r)
		return query(rt<<1,x);
	else
		return query(rt<<1|1,x);
}
int XLE,XRI,Xval;
void update(int rt){
	if (segtree[rt].l>XRI||segtree[rt].r





你可能感兴趣的:(————ACM相关————,——字符串处理——,ACM,-,AC自动机,——数据结构——,ACM,-,线段树)