zoukankan      html  css  js  c++  java
  • 2014 西安 G UVALive

    就是在建好的回文树上进行DFS计数。

    0,0代表偶数

    1,1代表奇数

    然后不断往后暴力添加字符,判断是否存在后继新的回文串节点,计数相称即可。

     1 #include <bits/stdc++.h>
     2 const long long mod = 1e9+7;
     3 const double ex = 1e-10;
     4 #define inf 0x3f3f3f3f
     5 using namespace std;
     6 const int MAXN = 200022;
     7 const int N = 26;
     8 char A[200022],B[200022];
     9 struct Palindromic_Tree{
    10     int next[MAXN][N];
    11     int fail[MAXN];
    12     int cnt[MAXN];
    13     int num[MAXN];
    14     int len[MAXN];
    15     int S[MAXN];
    16     int last;
    17     int n;
    18     int p;
    19     int newnode(int l){
    20         for (int i = 0; i < N; i++) next[p][i] = 0;
    21         cnt[p] = 0;
    22         num[p] = 0;
    23         len[p] = l;
    24         return p++;
    25     }
    26     void init(){
    27         p = 0;
    28         newnode(0);
    29         newnode(-1);
    30         last  = 0;
    31         n = 0;
    32         S[n] = -1;
    33         fail[0] = 1;
    34     }
    35     int get_fail(int x){
    36         while (S[n - len[x] - 1] != S[n]) x=fail[x];
    37         return x;
    38     }
    39     void add(int c){
    40         c -= 'a';
    41         S[++n] = c;
    42         int cur = get_fail(last);
    43         if (!next[cur][c]){
    44             int now = newnode(len[cur] + 2);
    45             fail[now] = next[get_fail(fail[cur])][c];
    46             next[cur][c] = now;
    47             num[now] = num[fail[now]] + 1;
    48         }
    49         last = next[cur][c];
    50         cnt[last]++;
    51     }
    52     void count(){
    53         for (int i = p - 1; i>=0; --i) cnt[fail[i]] += cnt[i];
    54     }
    55 };
    56 Palindromic_Tree a,b;
    57 long long dfs(int x,int y){
    58     long long tans = 0;
    59     for (int i = 0 ; i< 26 ; i++){
    60         if (a.next[x][i]&&b.next[y][i]){
    61             tans += 1LL*a.cnt[a.next[x][i]]*b.cnt[b.next[y][i]] + dfs(a.next[x][i],b.next[y][i]);
    62         }
    63     }
    64     return tans;
    65 }
    66 int main()
    67 {
    68     int T;
    69     scanf("%d",&T);
    70     for (int cas = 1; cas <= T; cas++){
    71         scanf(" %s",A);
    72         scanf(" %s",B);
    73         a.init();
    74         b.init();
    75         int la = strlen(A);
    76         int lb = strlen(B);
    77         for (int i = 0 ; i < la ; i++){
    78             a.add(A[i]);
    79         }
    80         for (int i = 0 ; i < lb ; i++){
    81             b.add(B[i]);
    82         }
    83         a.count();
    84         b.count();
    85         long long ans = 0;
    86         ans = dfs(0,0) + dfs(1,1);
    87         printf("Case #%d: %lld
    ",cas,ans);
    88     }
    89 }
    View Code
  • 相关阅读:
    高精度模板(未完待续)
    $CH0201$ 费解的开关
    $POJ2288$ $Islands$ $and$ $Bridges$
    luoguP1445 [Violet]樱花
    P3694 邦邦的大合唱站队
    [NOI2009]管道取珠
    [AHOI2006]基因匹配
    luogu P3411 序列变换
    HNOI2001 产品加工
    牛客ACM赛 B [小a的旅行计划 ]
  • 原文地址:https://www.cnblogs.com/HITLJR/p/7687885.html
Copyright © 2011-2022 走看看