2014西安区域赛G The Problem to Slow Down You 回文树

A A A B B B两个串中,相同的回文串的对数。

A A A B B B建立两棵回文树,然后记录每个点的次数(也就是回文串的个数),随后分别从两棵树对应的奇长度树和偶长度树向下遍历即可,遇到相同的节点就统计答案,否则停止搜索。

#define others
#ifdef poj
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#endif // poj
#ifdef others
#include <bits/stdc++.h>
#include <ext/rope>
#include <ext/pb_ds/priority_queue.hpp>
#endif // others
//#define file
#define all(x) x.begin(), x.end()
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define eps 1e-8
const double pi = acos(-1.0);


typedef long long LL;
typedef long long DLL;
typedef unsigned long long ULL;
void umax(LL &a, LL b) {
    a = max(a, b);
}
void umin(LL &a, LL b) {
    a = min(a, b);
}
int dcmp(double x) {
    return fabs(x) <= eps?0:(x > 0?1:-1);
}
void file() {
    freopen("data_in.txt", "r", stdin);
    fre`在这里插入代码片`open("data_out.txt", "w", stdout);
}

DLL mod = 1e9;

DLL Pow(DLL a,DLL b) {
    DLL res=1;
    a%=mod;
    for(; b; b>>=1) {
        if(b&1)res=res*a%mod;
        a=a*a%mod;
    }
    return res;
}
//
//void print(DLL x) {
//    if(x < 0) {
//        x = -x;
//        putchar('-');
//    }
//    if(x > 9) print(x/10);
//    putchar(x%10 + '0');
//}
//#define iostart
//#define iostart
#define pb(x) push_back(x)
namespace solver {
    /*
    all最后一个字符的位置
    len[x], 当前节点的回文串长度
    diff[x], 当前节点所在的等差数列的差
    top[x], 当前x节点所在等差数列的最短后缀回文串所在的节点
    last,当前最长回文后缀所在的节点
    f[i], 1~i组成的前缀串的最长回文后缀
    1. structure
    回文树有两个树结构,分别代表偶数长度的回文(0)和奇数长度的回文(-1)
    2. son
    每个节点都是一个本质不同的回文串
    son代表在当前回文串左右添加一个字符后变成的新的回文串
    3. fail
    fail指针指向当前节点的最长后缀回文串
    4. build
    每次插入字符只会把最长后缀回文串贡献到树中(根据对称,其他的后缀的贡献已经统计过了,
    每次最多添加一个新的节点,所以保证了回文串的个数是O(n)的级别)
    如果最长的加不上,那么就去试他的最长后缀回文串..不停跳fail
    5. 后缀回文串的长度之间形成log个等差数列
    */

    namespace ds {
        const int N=200010,S=26;
        char s[N];
        struct AP{
          int s,d,r,f;
          //k*d+s 0<=k<=r
          AP(){}
          AP(int _s,int _d,int _r,int _f){s=_s,d=_d,r=_r,f=_f;}
        }a[N];
        struct DS{
          int all,son[N][S],fail[N],len[N],diff[N],top[N],text[N],last,tot, f[N], times[N];
          //
          int newnode(int l){
            for(int i=0;i<S;i++)son[tot][i]=0;
            fail[tot]=diff[tot]=top[tot]=0,len[tot]=l,times[tot]=0;
            return tot++;
          }
          void init(){
            last=tot=all=0;
            newnode(0),newnode(-1);
            text[0]=-1,fail[0]=1;
          }
          int getfail(int x){
            while(text[all-len[x]-1]!=text[all])x=fail[x];
            return x;
          }
          void add(int w){
            w-='a';
            text[++all]=w;
            int x=getfail(last);
            if(!son[x][w]){
              int y=newnode(len[x]+2);
              int z=son[getfail(fail[x])][w];
              son[x][w]=y;
              fail[y]=z;
              diff[y]=len[y]-len[z];
              if(diff[y]!=diff[z]||len[z]<1)top[y]=y;else top[y]=top[z];
              //如果差值不相同,就开新的等差数列
            }
            last=son[x][w];
            f[all]=last;
            times[last]++;
          }
          void get_times() {
            for (int i = tot - 1; i >= 0; i--) {
                times[fail[i]] += times[i];
            }
          }
          void get(int x,AP q[],int&cnt){
            cnt=0;
            x=f[x];
            while(len[x]>0){
              int y=top[x];
              q[++cnt]=AP(len[y],diff[x],(len[x]-len[y])/diff[x],len[x]);
              /*
              长度为len[y]~len[x]的后缀都是回文后缀
              能够组成最多log个等差数列的形式,按照结构体的形式存储
              len[y]是起始长度, len[x]是最后一个位置的长度,diff[x]是公差,(len[x]-len[y])/diff[x]+1是数列长度
              */
              //printf("! %d %d %d %d\n",len[y],len[x],diff[x],(len[x]-len[y])/diff[x]);
              x=fail[y];
            }
          }
        } p, q;
        LL ans = 0;
        void get(int d1, int d2) {
//            cout << " debug : " << d1 << " " << d2 << " " << p.len[d1] << " " << q.len[d2] << " "<<p.times[d1]<<" "<<q.times[d2] <<endl;
            for (int i = 0; i < 26; i++){
                if (p.son[d1][i] && q.son[d2][i]) {
                    ans += 1LL*p.times[p.son[d1][i]]*q.times[q.son[d2][i]];
                    get(p.son[d1][i], q.son[d2][i]);
                }
            }
        }
        void build(int f) {
            ans = 0;
            p.init();
            q.init();
            string s1, s2;
            cin >> s1 >> s2;
            for (int i = 0; i < s1.size(); i++) {
                p.add(s1[i]);
            }
            for (int i = 0; i < s2.size(); i++) {
                q.add(s2[i]);
            }
            p.get_times();
            q.get_times();
            get(0, 0); get(1, 1);
            printf("Case #%d: %lld\n", f, ans);
        }
    };

    void solve() {
        int t;
        scanf("%d", &t);
        int f = 0;
        while (t--) {
            ds::build(++f);
        }
    }
}

int main() {
#ifdef iostart
    ios::sync_with_stdio(0);
    cin.tie(0);
#endif // iostart
//    file();
    solver::solve();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值