bzoj 4598: [Sdoi2016]模式字符串 点分治+hash

题意

给出n个结点的树结构T,其中每一个结点上有一个字符,这里我们所说的字符只考虑大写字母A到Z,再给出长度为m的模式串s,其中每一位仍然是A到z的大写字母。Alice希望知道,有多少对结点 <uv> <script type="math/tex" id="MathJax-Element-1"> </script>满足T上从u到V的最短路径形成的字符串可以由模式串S重复若干次得到?这里结点对 <uv> <script type="math/tex" id="MathJax-Element-2"> </script>是有序的,也就是说 <uv> <script type="math/tex" id="MathJax-Element-3"> </script>和 <vu> <script type="math/tex" id="MathJax-Element-4"> </script>需要被区分.所谓模式串的重复,是将若干个模式串S依次相接(不能重叠).例如当S=PLUS的时候,重复两次会得到PLUSPLUS,重复三次会得到PLUSPLUSPLUS,同时要注恿,重复必须是整数次的。例如当S=XYXY时,因为必须重复整数次,所以XYXYXY不能看作是S重复若干次得到的。
T<=10,n,M<=1000000

分析

刚看完题就往sam上想,想了半天发现搞不懂。瞄了一眼题解发现是点分治+hash,然后就会做了。
脑残细节题,调的我妈都不认得了。还好过了样例就一次A了,不然真的不知道要怎么调。。。
直接点分治,维护前缀后缀哈希,然后每次记录每种长度的前缀后缀数量,最后统计答案即可。

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;

typedef long long LL;
typedef unsigned long long ull;

const int N=1000005;

int n,len,cnt,last[N],a[N],ch[N],size[N],mx_size[N],pre[N],suf[N],tot_pre,tot_suf,stack[N];
int cnt_pre[N],cnt_suf[N],top,L1[N],L2[N],sum,root;
ull hash_pre[N],hash_suf[N],po[N],hash[N];
LL ans;
struct edge{int to,next;}e[N*2];
bool vis[N];
vector<int> vec;
char str[N];

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;
}

void addedge(int u,int v)
{
    e[++cnt].to=v;e[cnt].next=last[u];last[u]=cnt;
    e[++cnt].to=u;e[cnt].next=last[v];last[v]=cnt;
}

void get_root(int x,int fa)
{
    size[x]=1;mx_size[x]=0;
    for (int i=last[x];i;i=e[i].next)
    {
        if (e[i].to==fa||vis[e[i].to]) continue;
        get_root(e[i].to,x);
        size[x]+=size[e[i].to];
        mx_size[x]=max(mx_size[x],size[e[i].to]);
    }
    mx_size[x]=max(mx_size[x],sum-size[x]);
    if (!root||mx_size[x]<mx_size[root]) root=x;
}

bool check(int op,int top,int L)
{
    ull w=hash[stack[top]]-hash[stack[top-L]]*po[L];
    if (!op) return w==hash_pre[L];
    else return w==hash_suf[len-L+1];
}

void get(int x,int fa)
{
    hash[x]=hash[fa]*27+a[x];
    stack[++top]=x;
    if (top<len) L1[x]=check(0,top,top)?top:-1;
    else L1[x]=check(0,top,len)?L1[stack[top-len]]:-1;
    if (top<len) L2[x]=check(1,top,top)?top:-1;
    else L2[x]=check(1,top,len)?L2[stack[top-len]]:-1;
    if (L1[x]>-1) pre[++tot_pre]=L1[x];
    if (L2[x]>-1) suf[++tot_suf]=L2[x];
    size[x]=1;
    for (int i=last[x];i;i=e[i].next)
        if (e[i].to!=fa&&!vis[e[i].to]) get(e[i].to,x),size[x]+=size[e[i].to];
    top--;
}

void solve(int x)
{
    vis[x]=1;L1[x]=L2[x]=-1;
    if (a[x]==ch[1]) cnt_pre[1]++,vec.push_back(1),L1[x]=1;
    if (a[x]==ch[len]) cnt_suf[1]++,vec.push_back(1),L2[x]=1;
    for (int i=last[x];i;i=e[i].next)
    {
        if (vis[e[i].to]) continue;
        tot_pre=tot_suf=0;stack[top=1]=x;
        hash[x]=a[x];
        get(e[i].to,x);
        for (int j=1;j<=tot_pre;j++) ans+=cnt_suf[len-pre[j]+1];
        for (int j=1;j<=tot_suf;j++) ans+=cnt_pre[len-suf[j]+1];
        for (int j=1;j<=tot_pre;j++) cnt_pre[pre[j]]++,vec.push_back(pre[j]);
        for (int j=1;j<=tot_suf;j++) cnt_suf[suf[j]]++,vec.push_back(suf[j]);
    }
    for (int i=0;i<vec.size();i++) cnt_pre[vec[i]]=cnt_suf[vec[i]]=0;
    vec.clear();
    for (int i=last[x];i;i=e[i].next)
    {
        if (vis[e[i].to]) continue;
        sum=size[e[i].to];root=0;
        get_root(e[i].to,x);
        solve(root);
    }
}

void clear()
{
    cnt=ans=0;
    memset(last,0,sizeof(last));
    memset(vis,0,sizeof(vis));
}

int main()
{
    po[0]=1;
    for (int i=1;i<=1000000;i++) po[i]=po[i-1]*27;
    int T=read();
    while (T--)
    {
        n=read();len=read();
        clear();
        scanf("%s",str+1);
        for (int i=1;i<=n;i++) a[i]=str[i]-'A'+1;
        for (int i=1;i<n;i++)
        {
            int x=read(),y=read();
            addedge(x,y);
        }
        scanf("%s",str+1);
        for (int i=1;i<=len;i++) ch[i]=str[i]-'A'+1;
        hash_pre[0]=hash_suf[len+1]=0;
        for (int i=1;i<=len;i++) hash_pre[i]=hash_pre[i-1]+(ull)ch[i]*po[i-1];
        for (int i=len;i>=1;i--) hash_suf[i]=hash_suf[i+1]+(ull)ch[i]*po[len-i];
        root=0;sum=n;L1[0]=L2[0]=len;
        get_root(1,0);
        solve(root);
        printf("%lld\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值