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


Problem G. The Problem to Slow Down You
Description
After finishing his homework, our problem setter Federmann decided to kill time by hanging
around online. He found a cool chat room that discusses competitive programming. Federmann
has already joined lot of such chat rooms, but this one is special. Once he entered the
chat room, he noticed that there is an announcement saying “We forbid off-topic messages!”.
Federmann thinks that’s quite unusual, he decided to sit down and join the talk. After watching
people discussing different programming challenges for a while, he found an interesting
message saying “No, Federmann won’t prepare another problem about strings this year.”
“Oh, why do you guys think about that?” Federmann smiled. “Don’t they know I have
an Edward number2 of 3?”
He then thought about something about palindrome, given two strings A and B, what
is the number of their common palindrome substrings? The amount of common palindrome
substrings between two strings is defined as the number of quadruple (p, q, s, t), which satisfies
that:
1. 1 ≤ p, q ≤ length(A), 1 ≤ s, t ≤ length(B), p ≤ q and s ≤ t. Here length(A) means the
length of string A.
2. Ap..q = Bs..t
3. Ap..q is palindrome. (palindrome string is the string that reads the same forward or
backward)
For example, (1, 3, 1, 3) and (1, 3, 3, 5) are both considered as a valid common palindrome
substring between aba and ababa.
Federmann is excited about his new task, and he is just too lazy to write solutions, help
him.
Input
The first line of the input gives the number of test cases, T. T test cases follow. For each
test case, the first line contains a string A and the second line contains a string B. The length
of A, B will not exceed 200000.
It is guaranteed the input file will be smaller than 8 MB.
Output
For each test case, output one line containing “Case #x: y”, where x is the test case
number (starting from 1) and y is the number of common palindrome substrings of A and B.
2The Edward number is something like Erdős number, among problem setters.
12
The 2014 ACM-ICPC Asia Xi’an Regional Contest October 26, 2014
Samples
Sample Input Sample Output
3
abacab
abccab
faultydogeuniversity
hasnopalindromeatall
abbacabbaccab
youmayexpectedstrongsamplesbutnow
Case #1: 12
Case #2: 20
Case #3: 18

题目大意:问两个串中相同的回文子串有多少对

ac代码

13947920 2015-10-30 10:21:41 kxh1995 G - The Problem to Slow Down You MS C++ Accepted 343 ms 50100 KB

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#define maxn 200005
#define N 26
#define LL long long
using namespace std;
struct P_tree
{
    int next[maxn][N];
    int fail[maxn];
    int cnt[maxn];
    int len[maxn];
    int s[maxn];
    int n;
    int last;
    int p;
    int newnode(int l)
    {
        for(int i=0;i<N;i++)
            next[p][i]=0;
        cnt[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;
        }
        last=next[cur][c];
        cnt[last]++;
    }
    void count()
    {
        int i;
        for(i=p-1;i>=0;i--)
            cnt[fail[i]]+=cnt[i];
    }
}T1,T2;
char s1[maxn],s2[maxn];
int n1,n2;
LL ans;
void dfs(int u,int v)
{
    int i;
    for(i=0;i<26;i++)
    {
        int x=T1.next[u][i];
        int y=T2.next[v][i];
        if(x&&y)
        {
            ans+=(LL)T1.cnt[x]*(LL)T2.cnt[y];
            dfs(x,y);
        }
    }
}
void solve()
{
    ans=0;
    T1.init();
    T2.init();
    int n1=strlen(s1);
    int n2=strlen(s2);
    int i;
    for(i=0;i<n1;i++)
        T1.add(s1[i]);
    for(i=0;i<n2;i++)
        T2.add(s2[i]);
    T1.count();
    T2.count();
    dfs(0,0);
    dfs(1,1);
}
int main()
{
    int t,c=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s%s",s1,s2);
        printf("Case #%d: ",++c);
        solve();
        printf("%I64d\n",ans);
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值