CodeForces - 163E e-Government【Fail树&&AC自动机】

CodeForces - 163E

Time limit 1000 ms Memory limit 262144 kB

The best programmers of Embezzland compete to develop a part of the project called “e-Government” — the system of automated statistic collecting and press analysis.

We know that any of the k citizens can become a member of the Embezzland government. The citizens’ surnames are a1, a2, …, ak. All surnames are different. Initially all k citizens from this list are members of the government. The system should support the following options:

Include citizen ai to the government.
Exclude citizen ai from the government.
Given a newspaper article text, calculate how politicized it is. To do this, for every active government member the system counts the number of times his surname occurs in the text as a substring. All occurrences are taken into consideration, including the intersecting ones. The degree of politicization of a text is defined as the sum of these values for all active government members.
Implement this system.

题目大意

给定k个模式串,n次操作
每次操作可能是删除/增加第 i 个模式串(可能重复),或询问每个模式串在给定的文本串中出现次数的总和


题目分析

结合AC自动机和Fail树各自长处的好题

一开始k个模式串构造好Trie,并构造fail指针以及Fail树并求DFS序
对于没有被删除的模式串
利用差分数组,每个模式串结尾结点权值+1其子树中最后一个dfs序编号结点权值-1

每次询问将文本串送入AC自动机逐位匹配
若当前走到AC自动机结点 u u u,令 a n s + = q s u m ( 1 , d f n [ u ] ) ans+=qsum(1,dfn[u]) ans+=qsum(1,dfn[u])
这样即统计了该结点延fail指针走到根路径上 模式串结尾结点的个数(其他子树内的结点会因为差分数组抵消掉)

这样虽然不能准确统计每个模式串各自出现的次数
但将AC自动机和Fail树各自的特点巧妙地结合起来直接达到了统计总数的目的

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long lt;
#define lowbit(x) ((x)&(-x))

int read()
{
    int f=1,x=0;
    char ss=getchar();
    while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
    while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
    return x*f;
}

const int maxn=1000010;
int n,k,num;
int rem[maxn],cnt;
int ch[maxn][26],fail[maxn];
queue<int> q;
char pt[maxn];
struct edge{int v,nxt;}E[maxn];
int head[maxn],tot;
int dfn[maxn],size[maxn],judge[maxn],sz;
lt sum[maxn];

int get(char* ss)
{
	int x=0,len=strlen(ss);
    for(int i=0;i<len;++i) x=x*10+ss[i]-'0';
    return x;
}

void addE(int u,int v)
{
	E[++tot].nxt=head[u];
	E[tot].v=v;
	head[u]=tot;
}

lt qsum(int x){ lt res=0; for(int i=x;i>0;i-=lowbit(i))res+=sum[i]; return res;}
void add(int x,int v){ for(int i=x;i<=sz;i+=lowbit(i))sum[i]+=v;}
void update(int ll,int rr,int v){ add(ll,v); add(rr+1,-v);}

void ins(char* ss,int len,int d)
{
	int u=0;
	for(int i=0;i<len;++i)
	{
		int x=ss[i]-'a';
		if(!ch[u][x]) ch[u][x]=++cnt;
		u=ch[u][x];
	}
	rem[d]=u;
}

void getf()
{
    for(int i=0;i<26;++i)
    if(ch[0][i]) fail[ch[0][i]]=0,q.push(ch[0][i]);
    
    while(!q.empty())
    {
        int u=q.front(); q.pop();
        for(int i=0;i<26;++i)
        {
            if(!ch[u][i]) ch[u][i]=ch[fail[u]][i];
            else{
                fail[ch[u][i]]=ch[fail[u]][i];
                q.push(ch[u][i]);
            }
        }
    }
    
    for(int i=1;i<=cnt;++i)
    addE(fail[i],i);
}

void dfs(int u)
{
	dfn[u]=++sz; size[u]=1;
	for(int i=head[u];i;i=E[i].nxt)
	{
		int v=E[i].v;
		dfs(v);
		size[u]+=size[v];
	}
}

lt query(char*ss)
{
	lt ans=0;
	int u=0,len=strlen(ss);
	for(int i=0;i<len;++i)
	{
		u=ch[u][ss[i]-'a'];
		ans+=qsum(dfn[u]);
	}
	return ans;
}

int main()
{
	n=read();k=read();
	for(int i=1;i<=k;++i)
	{
		scanf("%s",&pt);
		ins(pt,strlen(pt),i);
	}
	
	getf(); dfs(0);
	for(int i=1;i<=k;++i)
	{
		update(dfn[rem[i]],dfn[rem[i]]+size[rem[i]]-1,1);
		judge[i]=1;
	}
	
	for(int i=1;i<=n;++i)
	{
		scanf("%s",&pt);
		if(pt[0]=='+')
		{
			int x=get(pt+1);
			if(judge[x]) continue;
			update(dfn[rem[x]],dfn[rem[x]]+size[rem[x]]-1,1);
			judge[x]=1;
		}
		else if(pt[0]=='-')
		{
			int x=get(pt+1);
			if(!judge[x]) continue;
			update(dfn[rem[x]],dfn[rem[x]]+size[rem[x]]-1,-1);
			judge[x]=0;
		}
		else if(pt[0]=='?')
		printf("%lld\n",query(pt+1)); 
	}
	
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子。为了使cnt_white - cnt_black尽可能大,可以使用两次形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的链所代表的子。在第二次遍历中,需要维护一个sum变量,用于存储链所代表的子的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是链所代表的子的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的链所代表的子。在第二次遍历中,需要维护一个sum变量,用于存储链所代表的子的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值