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. 杭电1879继续畅通project

    继续畅通project Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  2. 【cl】cmd相关命令

    cd  进入目录 dir  列出当前目录下的文件[在linux上是ls] e:  进入E盘 tab键可以快速进入目录

  3. linux下怎样将sheduler绑定到制定的cpu核上

    作者:张昌昌   1.顺序绑定 erl +sbt db 是按从前到后的顺序来绑定调度器的,如: erl +sbt db +S 3含义是启动erlang虚拟机,开启3个调度器,按顺序绑定在0,1.2号核 ...

  4. ES Segment Memory——本质上就是segment中加到内存的FST数据,因此segment越多,该内存越大

    ElasticSearch优化系列四:ES的heap是如何被瓜分掉的 转自:https://www.jianshu.com/p/f41b706db6c7 以下分别解读几个我知道的内存消耗大户: Seg ...

  5. Nginx调优实战

    Nginx配置文件性能微调 全局的配置 user www-data; pid /var/run/nginx.pid; worker_processes auto; worker_rlimit_nofi ...

  6. [JavaEE] 了解Java连接池

    转载自51CTO http://developer.51cto.com/art/201006/207768.htm 51CTO曾经为我们简单的介绍过Java连接池.要了解Java连接池我们先要了解数据 ...

  7. php和nodejs

    整个故事正如好莱坞大片的经典剧情走向:两位昔日好友如今分道扬镳,甚至被迫陷入了你死我活的斗争当中.刚开始的分歧并不严重,无非是一位老友对于另一位伙伴长久以来占据.但又绝口不提的业务领域产生了点兴趣.而 ...

  8. Selenium的文件上传JAVA脚本

    在写文件上传脚本的时候,遇到了很多问题,包括元素定位,以及上传操作,现在总结下来以下几点: 1. 上传的控件定位要准确,必要时要进行等待 WebElement adFileUpload = drive ...

  9. guice基本使用,配置模块的两种方式(三)

    guice是使用module进行绑定的,它提供了两种方式进行操作. 第一种是继承AbstractModule抽象类. package com.ming.user.test; import com.go ...

  10. Oracle-基本SQL语句

    --添加一个表 create table TestUser ( id int primary key , name varchar(20) , address varchar(20) ) /* *设置 ...