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处理一遍就可以了,然后顺着这两棵树的边走下去就 ...
随机推荐
- bzoj3931【CQOI2015】网络吞吐量
3931: [CQOI2015]网络吞吐量 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 853 Solved: 381 [Submit][id= ...
- S 禁止F12和右键操作控制台,兼容各浏览器
document.oncontextmenu = function () { return false; }; document.onkeydown = function () { ...
- 【JavaScript】——JS入门
结束XML之旅,開始JavaScript的学习,看视频.了解了她的前世今生,还是为她捏了把汗啊! 看了部分视 频了,简单的总结一下吧! JavaScript是什么? JavaScript是一种基于面向 ...
- 【cl】Unable to find executable for: taskkill
十二月 02, 2015 5:16:56 下午 org.openqa.selenium.os.ProcessUtils killWinProcess警告: Process refused to die ...
- 【CSS】CSS画矩形、圆、半圆、弧形、半圆、小三角、疑问框
在网页中,常常会用到各种Icon,假设老是麻烦设计狮画出来不免有些不好意思,所以有时候我们也能够用CSS写出各种简单的形状.一来能够减轻他们的负担,二来也能够使用CSS替代图片.提高载入速度. 在网页 ...
- python实现高速排序算法(两种不同实现方式)
# -*- coding: utf-8 -*- """ Created on Fri May 16 17:24:05 2014 @author: lifeix " ...
- 【CQOI 2009】 余数之和
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1257 [算法] k mod i = k - [k / i] * i 所以 (k mo ...
- 【读书笔记】UEFI原理与编程(1)概述及开发环境的搭建
一.概述: 0.为什么会有这篇文章 说实在的,在2016初的时候,我就萌生了写一个操作系统的念头,但是这对于我一个菜鸟来说,犹如登天. 既然想了就去写,即使最后做不完,也不后悔. 抱着这样的念头,我开 ...
- Linux目录结构(二)
Linux文件系统结的结构是树形结构,其入口从/开始,了解Linux文件系统的结构,对于我们需要掌握的基础知识点之一. 2.文件系统的组织结构简说: 当您使用Linux的时候,如果您通过ls -la ...
- 重温前端基础之-css浮动与清除浮动
文档流的概念指什么?有哪种方式可以让元素脱离文档流? 文档流,指的是元素排版布局过程中,元素会自动从左往右,从上往下的流式排列.并最终窗体自上而下分成一行行,并在每行中按从左到右的顺序排放元素.脱离文 ...