前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >UVAlive 7041 The Problem to Slow Down You(回文树)

UVAlive 7041 The Problem to Slow Down You(回文树)

作者头像
ShenduCC
发布2018-04-26 17:30:56
6880
发布2018-04-26 17:30:56
举报
文章被收录于专栏:算法修养算法修养

题目链接:

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5053

先把第一个串插入回文树中,然后把s数组清空插入第二个串,统计两个cnt数组,答案是二者相乘的结果

#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <string.h>

using namespace std;
typedef long long int LL;
const int maxn=2e5+5;
char str1[maxn];
char str2[maxn];
int n;
LL ans;
struct Tree
{
	const static int maxn=4e5+5;
    int next[maxn][26];
    int fail[maxn];
    LL  cnt[maxn];
    LL  cnt2[maxn];
    int len[maxn];
    int s[maxn];
    int last,p,n;
    int new_node(int x)
    {
        memset(next[p],0,sizeof(next[p]));
        cnt[p]=0;
        cnt2[p]=0;
        len[p]=x;
        return p++;
    }
       void init()
    {
        //memset(cnt,0,sizeof(cnt));
        //memset(cnt2,0,sizeof(cnt2));
        p=0;
        new_node(0);
        new_node(-1);
        last=0;
        n=0;
        s[0]=-1;
        fail[0]=1;
    }
    void init2()
    {
        last=0;
        s[0]=-1;
		fail[0]=1;
        n=0;
    }
    int get_fail(int x)
    {
        while(s[n-len[x]-1]!=s[n])
            x=fail[x];
        return x;
    }
    void add(int x)
    {
        x-='a';
        s[++n]=x;
        int cur=get_fail(last);
        if(!(last=next[cur][x]))
        {
            int now=new_node(len[cur]+2);
            fail[now]=next[get_fail(fail[cur])][x];
            next[cur][x]=now;
            last=now;
        }
        cnt[last]++;
    }
    void add2(int x)
    {
        x-='a';
        s[++n]=x;
        int cur=get_fail(last);
        if(!(last=next[cur][x]))
        {
            int now=new_node(len[cur]+2);
            fail[now]=next[get_fail(fail[cur])][x];
            next[cur][x]=now;
            last=now;
        }
        cnt2[last]++;
    }

    void count()
    {
        for(int i=p-1;i>=0;i--)
            cnt[fail[i]]+=cnt[i];
    }
    void count2()
    {
        for(int i=p-1;i>=0;i--)
            cnt2[fail[i]]+=cnt2[i];
    }
    void fun()
    {
        for(int i=2;i<=p-1;i++)
        {
            ans+=cnt[i]*cnt2[i];
        }
    }

}tree;
int main()
{
    scanf("%d",&n);
    for(int j=1;j<=n;j++)
    {
        scanf("%s%s",str1,str2);
        tree.init();
        int len=strlen(str1);
        int len1=strlen(str2);
        for(int i=0;i<len;i++)
        {
            tree.add(str1[i]);
        }
        tree.count();
        tree.init2();
        ans=0;
        for(int i=0;i<len1;i++)
        {
            tree.add2(str2[i]);
        }
        tree.count2();
        tree.fun();
        printf("Case #%d: %lld\n",j,ans);
    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-05-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档