bzoj 4598: [Sdoi2016]模式字符串

题意:

给一棵字母树和一个模式串,问有多少有序对(x,y)在树上路径是模式串的重复串。

题解:

因为从题解过来的,所以知道是点分+hash
点分的时候一个串显然只有最末/头的一小段是有用的,而且长度固定,直接用hash判掉即可。
code:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#define LL long long
#define LLu unsigned long long
using namespace std;
struct node{
    int y,next;
}a[200010];int len,last[100010];
const LLu base=200815147;
char str[100010];
int c[100010];
LLu pre[100010];
int n,vis[100010],m;
struct trnode{
    int tot,son;
}tr[100010];int root,sum;
LL ans;
void ins(int x,int y)
{
    a[++len].y=y;
    a[len].next=last[x];last[x]=len;
}
void get_sum(int x,int fa)
{
    sum++;
    for(int i=last[x];i;i=a[i].next)
        if(a[i].y!=fa&&!vis[a[i].y]) get_sum(a[i].y,x);
}
void find_root(int x,int fa)
{
    tr[x].tot=1;tr[x].son=0;
    for(int i=last[x];i;i=a[i].next)
    {
        int y=a[i].y;
        if(y==fa||vis[y]) continue;
        find_root(y,x);tr[x].tot+=tr[y].tot;
        if(tr[x].son<tr[y].tot) tr[x].son=tr[y].tot;
    }
    tr[x].son=max(tr[x].son,sum-tr[x].tot);
    if(!root||tr[root].son>tr[x].son) root=x;
}
LLu s[100010],d[100010],now[100010],s1[100010],d1[100010];
int lh[100010],rh[100010];
int max_len;
LLu get(LLu *a,int l,int r) {return a[r]-a[l-1]*pre[r-l+1];}
void check_lh(int len)
{
    int num=len/m,le=len-num*m;
    if((num==0||d[num]==get(now,le+1,len))&&(le==0||now[le]==get(s,m-le+1,m))) lh[le]++,max_len=max(max_len,le);
}
void check_rh(int len)
{
    if(len==1) {rh[0]++;return;}
    int num=(len-1)/m,le=len-num*m;
    if((num==0||d1[num]==get(now,le+1,len))&&(le<=1||get(now,2,le)==get(s1,m-le+2,m))) rh[le-1]++,max_len=max(max_len,le-1);
}
void dfs(int x,int fa,int d)
{
    now[d]=now[d-1]*base+c[x];
    check_lh(d);check_rh(d);
    for(int i=last[x];i;i=a[i].next)
        if(a[i].y!=fa&&!vis[a[i].y]) dfs(a[i].y,x,d+1);
    now[d]=0;
}
void calc(int op)
{
    ans=ans+op*(LL)lh[0]*(LL)rh[0];
    for(int i=1;i<=max_len;i++) ans=ans+op*(LL)lh[i]*rh[m-i];
    for(int i=0;i<=max_len;i++) lh[i]=rh[i]=0;
}
void solve(int x)
{
    if(sum<m) return;
    vis[x]=1;max_len=0;
    dfs(x,0,1);calc(1);
    for(int i=last[x];i;i=a[i].next)
    {
        int y=a[i].y;
        if(vis[y]) continue;
        max_len=0;now[1]=c[x];
        dfs(y,x,2);calc(-1);
        root=sum=0;get_sum(y,x);
        find_root(y,0);solve(root);
    }
}
int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int main()
{
    int T;T=read();
    pre[0]=1;for(int i=1;i<=100000;i++) pre[i]=pre[i-1]*base;
    while(T--)
    {
        n=read();m=read();
        scanf("%s",str+1);
        for(int i=1;i<=n;i++) c[i]=str[i]-'A';
        len=0;memset(last,0,sizeof(last));
        for(int i=1;i<n;i++)
        {
            int x,y;x=read();y=read();
            ins(x,y);ins(y,x);
        }
        scanf("%s",str+1);
        s[0]=0;for(int i=1;i<=m;i++) s[i]=s[i-1]*base+(str[i]-'A');
        d[1]=s[m];for(int i=2;i<=n/m;i++) d[i]=d[i-1]*pre[m]+s[m];
        s1[0]=0;for(int i=1;i<=m;i++) s1[i]=s1[i-1]*base+(str[m-i+1]-'A');
        d1[1]=s1[m];for(int i=2;i<=n/m;i++) d1[i]=d1[i-1]*pre[m]+s1[m];
        memset(vis,0,sizeof(vis));
        root=0;sum=n;ans=0;
        find_root(1,0);solve(root);
        printf("%lld\n",ans);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值