2014ACM-ICPC亚洲区域西安赛区 G题 / GYM 100548 G

Problem G. The Problem to Slow Down YouDescriptionAfter finishing his homework, our problem setter Federmann decided to kill time by hangingaround online. He found a cool chat room that discusses competitive programming. Federmannhas already joined lot of such chat rooms, but this one is special. Once he entered thechat 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 watchingpeople discussing different programming challenges for a while, he found an interestingmessage 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 havean Edward number2 of 3?”He then thought about something about palindrome, given two strings A and B, whatis the number of their common palindrome substrings? The amount of common palindromesubstrings between two strings is defined as the number of quadruple (p, q, s, t), which satisfiesthat:1. 1 ≤ p, q ≤ length(A), 1 ≤ s, t ≤ length(B), p ≤ q and s ≤ t. Here length(A) means thelength of string A.2. Ap..q = Bs..t3. Ap..q is palindrome. (palindrome string is the string that reads the same forward orbackward)For example, (1, 3, 1, 3) and (1, 3, 3, 5) are both considered as a valid common palindromesubstring between aba and ababa.Federmann is excited about his new task, and he is just too lazy to write solutions, helphim.InputThe first line of the input gives the number of test cases, T. T test cases follow. For eachtest case, the first line contains a string A and the second line contains a string B. The lengthof A, B will not exceed 200000.It is guaranteed the input file will be smaller than 8 MB.OutputFor each test case, output one line containing “Case #x: y”, where x is the test casenumber (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.12The 2014 ACM-ICPC Asia Xi’an Regional Contest October 26, 2014SamplesSample Input Sample Output3abacababccabfaultydogeuniversityhasnopalindromeatallabbacabbaccabyoumayexpectedstrongsamplesbutnowCase #1: 12Case #2: 20Case #3: 18

思路:

利用回文树处理出两个串的每个回文串在该串中出现的次数,然后对回文树中的每个节点,用该节点所对应的回文串在第一个串中的出现次数乘上在第二个串中的出现次数,求和即可。

处理出每个串中的每个回文串在该串中出现次数的方法是:考虑到回文树中的每个节点对应一个回文字串,先用第一个串构造出回文树,插入两个不同的没有出现的字符(使得两个串不相干),再插入第二个串,过程中分别记录每个节点在第一个串和第二个串的出现次数。然后按照每个节点的len值(也就是所对应的回文串长度)排序,按len值从大到小的顺序通过回文后缀链回推。

例如当前节点对应串为ababa,其出现次数为2,则令它的最长回文后缀,及aba所对应的节点的出现次数+2。

在举个例子:

对串ababa,第一次构造完回文树后,每个节点对应回文串出现次数为:

a:1次

b:1次

aba:1次

bab:1次

ababa:1次

排序后进行反推:

对ababa ,使得aba出现次数+1

对bab,使得b出现次数+1

对aba,其出现次数变为2,使得a出现次数+2

对b和a,忽略其影响

最终得到各回文串的正确的出现次数:

a:3次

b:2次

aba:2次

bab:1次

ababa:1次


于是问题就解决了。注意结果会爆int,还有数据中的字符只有小写英文字母。

#include <iostream>
#include <cstdio>
#include <map>
#include <cstring>
#include <string>
#include <queue>

#define M 110
#define N 210000
using namespace std;

class Node
{
public:
    int pre, next[30], num[2], len, preNum, val;
    void init(int _len)
    {
        len = _len;
        pre = 0;
        memset(next, 0, sizeof next);
        num[0] = num[1] = 0;
        preNum = 0;
        val = 0;
    }
};
Node node[N*2];
int bucket[N*2], sorted[N*2];

class PaliTree
{
public:
    int cnt, root0, root1, last, n;
    int str[N];
    long long sum;
    int newNode(int _len)
    {
        node[++cnt].init(_len);
        return cnt;
    }
    void init()
    {
        cnt = -1;
        root0 = newNode(0);
        root1 = newNode(-1);
        last = root0;
        sum = 0;
        n = 0;
        str[0] = -1;
        node[root0].pre = root1;
    }
    int getFail(int p)
    {
        for(; str[n-node[p].len-1] != str[n]; p = node[p].pre);
        return p;
    }
    void extend(int ch, int flag)
    {
        str[++n] = ch;
        int cur = getFail(last);
        int nxt = node[cur].next[ch];
        if(!nxt)
        {
            nxt = newNode(node[cur].len+2);
            node[nxt].pre = node[getFail(node[cur].pre)].next[ch];
            node[cur].next[ch] = nxt;
        }
        node[nxt].num[flag]++;
        last = nxt;
    }
    long long solve()
    {
        int maxLen = -1;
        memset(bucket, 0, sizeof bucket);
        for(int i = 2; i <= cnt; i++)
        {
            bucket[node[i].len]++;
            maxLen = max(maxLen, node[i].len);
        }
        for(int i = 1; i <= maxLen; i++)
            bucket[i] += bucket[i-1];

        for(int i = cnt; i >= 2; i--)
        {
            sorted[--bucket[node[i].len]] = i;
        }
        for(int i = cnt-2; i >= 0; i--)
        {
            node[node[sorted[i]].pre].num[0] += node[sorted[i]].num[0];
            node[node[sorted[i]].pre].num[1] += node[sorted[i]].num[1];
        }
        long long sum = 0;
        for(int i = 2; i <= cnt; i++)
            sum += (long long)node[i].num[0]*(long long)node[i].num[1];
        return sum;
    }

};
PaliTree tree;
char s[N];
int main()
{
	//freopen("C:\\Users\\zfh\\Desktop\\in.txt", "r", stdin);
    int t; scanf("%d", &t);
    int cas = 0;
    while(t--)
    {
        scanf(" %s", &s);
        int len = strlen(s);
        tree.init();
        for(int i = 0; i < len; i++)
        {
            int temp = s[i]-'a';
            tree.extend(temp, 0);
        }
        tree.extend(28, 0);
        tree.extend(29, 0);
        scanf(" %s", &s);
        len = strlen(s);
        for(int i = 0; i < len; i++)
        {
            int temp = s[i]-'a';
            tree.extend(temp, 1);
        }

        printf("Case #%d: %I64d\n", ++cas, tree.solve());
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值