The Problem to Slow Down You

Time Limit: 20000ms
Memory Limit: 524288KB

This problem will be judged on CodeForcesGym. Original ID: 100548G
64-bit integer IO format: %I64d      Java class name: (Any)

 
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.

Samples

Sample Input

3

abacab

abccab

faultydogeuniversity

hasnopalindromeatall

abbacabbaccab

youmayexpectedstrongsamplesbutnow

Sample Output

Case #1: 12

Case #2: 20

Case #3: 18

Source

解题:回文机

 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = ;
const int N = ;
struct PalindromicTree {
int next[maxn][N],cnt[maxn],fail[maxn];
int len[maxn],s[maxn],num[maxn];
int tot,last,n;
int newnode(int length) {
memset(next[tot],,sizeof next[tot]);
len[tot] = length;
cnt[tot] = num[tot] = ;
return tot++;
}
int get_fail(int x) {
while(s[n - len[x] - ] != s[n]) x = fail[x];
return x;
}
void init() {
last = tot = n = ;
newnode();
newnode(-);
fail[] = ;
s[n] = -;
}
void count() {
for(int i = tot-; i >= ; --i)
cnt[fail[i]] += cnt[i];
}
void add(int c) {
c -= 'a';
s[++n] = c;
int cur = get_fail(last);
if(!next[cur][c]) {
int now = newnode(len[cur] + );
fail[now] = next[get_fail(fail[cur])][c];
next[cur][c] = now;
num[now] = num[fail[now]] + ;
}
last = next[cur][c];
cnt[last]++;
}
} a,b;
LL ret;
void dfs(int u,int v) {
for(int i = ; i < N; ++i) {
int x = a.next[u][i];
int y = b.next[v][i];
if(x && y) {
ret += (LL)a.cnt[x]*b.cnt[y];
dfs(x,y);
}
}
}
char str[maxn];
int main() {
int kase,cs = ;
scanf("%d",&kase);
while(kase--) {
a.init();
b.init();
scanf("%s",str);
for(int i = ; str[i]; ++i) a.add(str[i]);
scanf("%s",str);
for(int i = ; str[i]; ++i) b.add(str[i]);
a.count();
b.count();
ret = ;
dfs(,);
dfs(,);
printf("Case #%d: %I64d\n",cs++,ret);
}
return ;
}

CodeForcesGym 100548G The Problem to Slow Down You的更多相关文章

  1. Gym - 100548G The Problem to Slow Down You

    依然是回文树. 我们只需要吧siz[]改成统计两边的siz[][0/1],然后把两个字符中间随便加一个不会出现的字符拼起来,做一遍回文树统计一下就OJBK了 #include<bits/stdc ...

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

    The Problem to Slow Down You Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjud ...

  3. 回文自动机 + 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.actio ...

  4. The Problem to Slow Down You

    The Problem to Slow Down You 输入:t个测试样例,每个样例输入两个字符串 输出:这两对字符串的回文串可以组成多少对本质不同的回文串 题意:给你两个字符串,然后问你这两字符串 ...

  5. CodeForcesGym 100524A Astronomy Problem

    Astronomy Problem Time Limit: 8000ms Memory Limit: 524288KB This problem will be judged on CodeForce ...

  6. UVALive - 7041 The Problem to Slow Down You (回文树)

    https://vjudge.net/problem/UVALive-7041 题意 给出两个仅包含小写字符的字符串 A 和 B : 求:对于 A 中的每个回文子串,B 中和该子串相同的子串个数的总和 ...

  7. UVAlive 7041 The Problem to Slow Down You(回文树)

    题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...

  8. Codeforces.GYM100548G.The Problem to Slow Down You(回文树)

    题目链接 \(Description\) 给定两个串\(S,T\),求两个串有多少对相同回文子串. \(|S|,|T|\leq 2\times 10^5\). \(Solution\) 好菜啊QAQ ...

  9. 2014-2015 ACM-ICPC, Asia Xian Regional Contest GThe Problem to Slow Down You

    http://blog.csdn.net/u013368721/article/details/42100363  回文树 建立两棵回文树,然后count处理一遍就可以了,然后顺着这两棵树的边走下去就 ...

随机推荐

  1. 关于联想超极本出现蓝屏Default Boot Device Missing or Boot Failed的解决办法

    联想笔记本出现以下症状无法开机时: 解决方案:恢复BIOS默认设置,把硬盘设置为第一启动项. 若成功检测到硬盘并有EFI引导程序,那么恭喜你这就完事了,重启后就可以正常开机了. 但是,若在UEFI模式 ...

  2. xcode Automatic signing is unable to resolve an issue with the "ShowCar-IOS" target's entitlements

    1.https://stackoverflow.com/questions/37806538/code-signing-is-required-for-product-type-application ...

  3. SQL SERVER读书笔记:阻塞与死锁

    阻塞是事务隔离带来的副作用,而并不是SQL SERVER的错. 死锁则是互相争用资源而引发.由于死锁会选择牺牲者,所以死锁的危害没有阻塞大.但有时为了解决死锁,会采取对资源加锁,导致阻塞的方式来避免.

  4. POJ1837 Balance 背包

    题目大意: 有一个天平,天平左右两边各有若干个钩子,总共有C个钩子(每个钩子有相对于中心的距离,左负右正),有G个钩码,求将钩码全部挂到钩子上使天平平衡的方法的总数. 将每个砝码看作一组,组内各个物品 ...

  5. 名词解释 —— 抄送(cc)、银弹(silver bullet)

    抄送(Carbon Copy,carbon copy 本身的含义是复写本,副本),又简称为 CC. 在现代汉语中,含有抄写与送达的双重意思. 在网络术语中,抄送就是将邮件同时发送给收信人以外的人, 用 ...

  6. Linux Shell Scripting Cookbook 读书笔记 5

    sed,awk 1. sed (string editor) 使用-i可以将结果运用于原文件 sed 's/text1/text2/' file > newfile mv newfile fil ...

  7. HDU2080 夹角有多大2

    2019-05-17 15:00:09 加油加油,fightting !!! 这道题不知道acos()函数,acos()返回的是弧度,转化成度数要 / PI * 180 也没有想到通过向量 但是想到了 ...

  8. 前端之HEML

    HTML介绍 Web服务本质   import socket sk = socket.socket() sk.bind(("127.0.0.1", 8080)) sk.listen ...

  9. 工厂方法模式(Product)C++实现

    意图:定义一个用于创建对象的接口,让子类觉定实例化哪一个类. 适用性:1.一个类不知道它必须创建的对象的时候. 2.一个类希望由它的子类指定它所创建的对象的时候. 3.当类将创建对象的职责委托给多个帮 ...

  10. javascript 公历与农历相互转换工具类

    /** * 公历[1900-1-31,2100-12-31]时间区间内的公历.农历互转 * @charset UTF-8 * @Author Jea杨(JJonline@JJonline.Cn) * ...