回文自动机 + DFS --- The 2014 ACM-ICPC Asia Xi’an Regional Contest Problem G.The Problem to Slow Down You

简介: The Problem to Slow Down You  Problem's Link:  http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=141572   Mean:  给你两个字符串,求这两个字符串相同回文串的匹配对数。

The Problem to Slow Down You 

Problem's Link:  http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=141572


 

Mean: 

给你两个字符串,求这两个字符串相同回文串的匹配对数。

analyse:

每个字符串建一棵回文树,分别从0结点和1结点两棵树一起往下dfs,对于同一条路径上的结点,一定是相同的回文,然后两个的数量相乘加到answer中。

Time complexity: O(N)

 

Source code: 

/*
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-08-22-13.43
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define  LL long long
#define  ULL unsigned long long
using namespace std;

const int MAXN = 200050 ;
const int N = 26;
LL ans;
char s [ MAXN ];
struct Palindromic_Tree
{
      int next [ MAXN ][N ];
      int fail [ MAXN ];
      int cnt [ MAXN ];
      int num [ MAXN ];
      int len [ MAXN ];
      int S [ MAXN ];
      int last;
      int n ;
      int p ;
      int newnode( int l)
      {
            for( int i = 0 ; i < N ; ++ 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)
      {
            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 ] ;
                  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 ]; }
} t1 , t2;

void dfs( int u , int v)
{
      for( int i = 0; i <N; ++ i)
      {
            int x = t1 . next [ u ][ i ];
            int y = t2 . next [ v ][ i ];
            if( x && y)
            {
                  ans +=( LL) t1 . cnt [ x ] * t2 . cnt [ y ];
                  dfs( x , y);
            }
      }
}

int main()
{
      int t;
      scanf( "%d" , & t);
      for( int Cas = 1; Cas <= t; ++ Cas)
      {
            t1 . init (), t2 . init();
            scanf( "%s" ,s);
            for( int i = 0; s [ i ]; ++ i) t1 . add(s [ i ]);
            scanf( "%s" ,s);
            for( int i = 0; s [ i ]; ++ i) t2 . add(s [ i ]);
            t1 . count (), t2 . count();
            ans = 0;
            dfs( 0 , 0 ), dfs( 1 , 1);
            printf( "Case #%d: %lld \n " , Cas , ans);
      }
      return 0;
}
目录
相关文章
|
7月前
|
人工智能 算法 ice
【2024美赛】D题(中英文):五大湖水资源问题Problem Problem D: Great Lakes Water Problem
【2024美赛】D题(中英文):五大湖水资源问题Problem Problem D: Great Lakes Water Problem
99 1
Leetcode 365. Water and Jug Problem
一句话理解题意:有容积为x和y升的俩水壶,能不能量出z升的水。 我刚开始看到这题,立马就想了下暴力搜索的可能性,但考虑了下数据大小,立马放弃这个暴力的想法,于是意识到肯定有比较简单的数学方法,其实我自己没想到,后来看还是看了别人的代码,很多博客都直接给出了解法, 但没介绍为什么能这么解。所以我决定解释下我自己的思路。
49 0
2021 ICPC Asia Regionals Online Contest (II) Problem G. Limit
2021 ICPC Asia Regionals Online Contest (II) Problem G. Limit
The Preliminary Contest for ICPC China Nanchang National Invitational A题 PERFECT NUMBER PROBLEM
The Preliminary Contest for ICPC China Nanchang National Invitational A题 PERFECT NUMBER PROBLEM
72 0
|
机器学习/深度学习 人工智能
The Preliminary Contest for ICPC China Nanchang National Invitational I题 Max answer
The Preliminary Contest for ICPC China Nanchang National Invitational I题 Max answer
95 0
|
机器学习/深度学习
The Preliminary Contest for ICPC China Nanchang National Invitational J题 Distance on the tree
The Preliminary Contest for ICPC China Nanchang National Invitational J题 Distance on the tree
94 0
LeetCode 365. Water and Jug Problem
有两个容量分别为 x升 和 y升 的水壶以及无限多的水。请判断能否通过使用这两个水壶,从而可以得到恰好 z升 的水?
84 0
LeetCode 365. Water and Jug Problem
2020 ICPC Asia Taipei-Hsinchu Site Programming Contest H. Optimization for UltraNet (二分+最小生成树+算贡献)
2020 ICPC Asia Taipei-Hsinchu Site Programming Contest H. Optimization for UltraNet (二分+最小生成树+算贡献)
128 0
lecture 3.2 problem set 3
"Radioactive decay" is the process by which an unstable atom loses energy and emits ionizing particles - what is commonly refered to as radiation.
1140 0