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
#include
#include