2014-2015 ACM-ICPC, Asia Xian Regional Contest G题The Problem to Slow Down You(回文树)

题目大意:

给了两个字符串,问有多少个字符串对,这个字符串对要求,S1出现在第一个字符串,S2出现在第二个字符串且S1=S2,并且是回文串。

思路:

看到题目就想到回文树了,然而一次这种题目都没有写过,导致我后来才发现我网上抄的模板是假的= =||。

创建两个回文树,分别存下两个字符串,然后分别对它们的奇根和偶根跑一遍,把所有存在在两个回文树里的并且相等的串都统计一下就好了。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
const int maxn=2e5+5;
char str[maxn];
typedef long long ll;

using namespace std;
ll ans=0;
struct Palindromic_Tree
{
    int next[maxn][26] ;//next指针,next指针和字典树类似,指向的串为当前串两端加上同一个字符构成
    int fail[maxn] ;//fail指针,失配后跳转到fail指针指向的节点
    int cnt[maxn] ; //表示节点i表示的本质不同的串的个数(建树时求出的不是完全的,最后count()函数跑一遍以后才是正确的)
    int num[maxn] ; //表示以节点i表示的最长回文串的最右端点为回文串结尾的回文串个数
    int len[maxn] ;//len[i]表示节点i表示的回文串的长度(一个节点表示一个回文串)
    int S[maxn] ;//存放添加的字符
    int last ;//指向新添加一个字母后所形成的最长回文串表示的节点。
    int n ;//表示添加的字符个数。
    int p ;//表示添加的节点个数。

    int newnode ( int l )  //新建节点
    {
        for ( int i = 0 ; i < 26 ; i++ )
            next[p][i] = 0 ;
        cnt[p] = 0 ;
        num[p] = 0 ;
        len[p] = l ;
        return p ++ ;
    }

    void init ()  //初始化
    {
        p = 0 ;
        newnode (  0 ) ;
        newnode ( -1 ) ;
        last = 0 ;
        n = 0 ;
        S[n] = -1 ;//开头放一个字符集中没有的字符,减少特判
        fail[0] = 1 ;
    }

    int get_fail ( int x )  //和KMP一样,失配后找一个尽量最长的
    {
        while ( S[n - len[x] - 1] != S[n] )
            x = fail[x] ;
        return x ;
    }

    void add ( int c )
    {
        c -= 'a' ;
        S[++ n] = c ;
        int cur = get_fail ( last ) ;//通过上一个回文串找这个回文串的匹配位置
        if ( !next[cur][c] )  //如果这个回文串没有出现过,说明出现了一个新的本质不同的回文串
        {
            int now = newnode ( len[cur] + 2 ) ;//新建节点
            fail[now] = next[get_fail ( fail[cur] )][c] ;//和AC自动机一样建立fail指针,以便失配后跳转
            next[cur][c] = now ;
            num[now] = num[fail[now]] + 1 ;
        }
        last = next[cur][c] ;
        cnt[last] ++ ;
    }

    void count ()
    {
        for ( int i = p - 1 ; i >= 0 ; -- i )
            cnt[fail[i]] += cnt[i] ;
        //父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串!
    }
} tree1,tree2;
void dfs(int x,int y)
{
    for(int i=0; i<26; i++)
    {
        int son1=tree1.next[x][i],son2=tree2.next[y][i];
        if(son1&&son2)
        {
            ans+=(ll)tree1.cnt[son1]*(ll)tree2.cnt[son2];
            dfs(son1,son2);
        }
    }
}

int main()
{
    int t,cas=1;
    scanf("%d",&t);
    while(t--)
    {
        ans=0;
        scanf("%s",str);
        tree1.init(),tree2.init();
        int len=strlen(str);
        for(int i=0; i<len; i++)
        {
            tree1.add(str[i]);
        }
        scanf("%s",str);
        len=strlen(str);
        for(int i=0; i<len; i++)
        {
            tree2.add(str[i]);
        }
        tree1.count();
        tree2.count();
        dfs(0,0);
        dfs(1,1);
        printf("Case #%d: ",cas++);
        printf("%lld\n",ans);
    }
    return 0;
}
/*
3
abacab
abccab
faultydogeuniversity
hasnopalindromeatall
abbacabbaccab
youmayexpectedstrongsamplesbutnow
*/

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值