CodeForcesGym 100548G The Problem to Slow Down You
The Problem to Slow Down You
This problem will be judged on CodeForcesGym. Original ID: 100548G
64-bit integer IO format: %I64d Java class name: (Any)
“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的更多相关文章
- Gym - 100548G The Problem to Slow Down You
依然是回文树. 我们只需要吧siz[]改成统计两边的siz[][0/1],然后把两个字符中间随便加一个不会出现的字符拼起来,做一遍回文树统计一下就OJBK了 #include<bits/stdc ...
- 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 ...
- 回文自动机 + 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 ...
- The Problem to Slow Down You
The Problem to Slow Down You 输入:t个测试样例,每个样例输入两个字符串 输出:这两对字符串的回文串可以组成多少对本质不同的回文串 题意:给你两个字符串,然后问你这两字符串 ...
- CodeForcesGym 100524A Astronomy Problem
Astronomy Problem Time Limit: 8000ms Memory Limit: 524288KB This problem will be judged on CodeForce ...
- UVALive - 7041 The Problem to Slow Down You (回文树)
https://vjudge.net/problem/UVALive-7041 题意 给出两个仅包含小写字符的字符串 A 和 B : 求:对于 A 中的每个回文子串,B 中和该子串相同的子串个数的总和 ...
- UVAlive 7041 The Problem to Slow Down You(回文树)
题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...
- Codeforces.GYM100548G.The Problem to Slow Down You(回文树)
题目链接 \(Description\) 给定两个串\(S,T\),求两个串有多少对相同回文子串. \(|S|,|T|\leq 2\times 10^5\). \(Solution\) 好菜啊QAQ ...
- 2014-2015 ACM-ICPC, Asia Xian Regional Contest GThe Problem to Slow Down You
http://blog.csdn.net/u013368721/article/details/42100363 回文树 建立两棵回文树,然后count处理一遍就可以了,然后顺着这两棵树的边走下去就 ...
随机推荐
- Ubuntu下启动Eclipse报错:A Java RunTime Environment (JRE) or Java Development Kit (JDK) must
原以为是jdk的环境变量配置错误了,于是从网上找了各种配置环境变量的方法.也注意空格的问题,可无论怎么改,还是这样报错!后来在网上看到一种奇怪的方法.我也不知道为什么这样就OK了? 方法:进入你的ec ...
- maven+springMVC+mybatis+easyUI管理用户增删改查
1.项目演示图 2.项目简单介绍 项目分为两个projectdomain和manager.project结构例如以下图所看到的.当中domain是Maven javaproject主要完毕对数据库的操 ...
- day63-webservice 08.在web项目中配置带有接口的webservice服务
这个是配置带有接口的WebService的服务. http://localhost:8080/cxf-web-server/service 带有接口的实现类也给它做好了.jaxws:endpoint是 ...
- js 数组包含
function(arr,element){ return new RegExp('(^|,)'+element.toString()+'(,|$)').test(arr.toString()); }
- Lucene中Analyzer语句分析
Lucene中Analyzer语句分析,利用lucene中自带的词法分析工具Analyzer,进行对句子的分析. 源代码如下: package com.test; import java.io.IOE ...
- BZOJ 1041 数学
思路: $x^2+y^2=r^2$$y=\sqrt{(r+x)(r-x)}$令$ d=gcd(r+x,r-x)$设A=$(r-x)/d$ $B=(r+x)/d$则$gcd(A,B)=1$$y^2=d^ ...
- A - Dubstep
Problem description Vasya works as a DJ in the best Berland nightclub, and he often uses dubstep mus ...
- guice基本使用,guice整合guice-servlet,web scope注解(六)
guice servlet提供了几个比较有用的web scope,类似与传统servlet 的session,request这些提供的范围等. guice servlet 提供的web scope 如 ...
- java的重载总结
1.不能以返回值的不同来重载方法,编译都不通过(只有参数类型或者参数个数不同才可以重载方法) 在Java语言中,要重载一个方法,除了要与原方法具有相同的简单名称外,还要求必须拥有一个与原方法不同的(不 ...
- Android 低功耗蓝牙的多设备连接与数据接收,简单实现
在网络层,互联网提供所有应用程序都要使用的两种类型的服务,尽管目前理解这些服务的细节并不重要,但在所有TCP/IP概述中,都不能忽略他们: 无连接分组交付服务(Connectionless Packe ...