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处理一遍就可以了,然后顺着这两棵树的边走下去就 ...
随机推荐
- Think In java 笔记一
本博客不再更新,很多其它精彩内容请訪问我的独立博客 今天起要读这本书了,曾经都没有认真读过书.是时候改变自己了. 如今认为不是写不出代码,而是没有想法,没有架构,要做一个大神不是写多少代码.而是要能做 ...
- iOS 加入粘贴板的功能(复制功能)
1. UIPasteboard *board = [UIPasteboard generalPasteboard]; board.string = @" 须要粘贴的问题字符串" ...
- oc5--方法
// main.m // 第一个OC类-方法2 #import <Foundation/Foundation.h> // 1.编写类的声明 @interface Iphone : NSOb ...
- sql server 2012中red gate的sql source control消失
找到C:\ProgramData\Microsoft\SQL Server Management Studio\11.0\Addins路径,用notepad++打开RedGate.SIPFramewo ...
- 国外物联网平台初探(六) ——Electric Imp
公司背景 Electric Imp成立于2011年,公司设立在美国加利福尼亚州洛斯阿尔托斯和英国剑桥 公司投资者包括:富士康技术集团.PTI创投.Rampart资本.Redpoint创投 定位 Ele ...
- mvc:view-controller直接转发页面
在springMVC中,通过@RequestMapping发送请求地址,转发到目标页面,但是,有时候想直接访问页面, 不想通过xxx.jsp直接访问页面,可以通过springmvc.xml配置文件中的 ...
- linux下nmap工具的使用
NMap,也就是Network Mapper,是Linux下的网络扫描和嗅探工 具包,其基本功能有三个,一是探测一组主机是否在线:其次是扫描主机端口,嗅探所提供的网络服务:还可以推断主机所用的操作系统 ...
- docker(部署常见应用):docker部署redis
上节回顾:docker(部署常见应用):docker部署mysql docker部署redis:4.0 # 下载镜像 docker pull redis:4.0 # 查看下载镜像 docker ima ...
- Hadoop MapReduce编程 API入门系列之wordcount版本2(六)
这篇博客,给大家,体会不一样的版本编程. 代码 package zhouls.bigdata.myMapReduce.wordcount4; import java.io.IOException; i ...
- java的重载总结
1.不能以返回值的不同来重载方法,编译都不通过(只有参数类型或者参数个数不同才可以重载方法) 在Java语言中,要重载一个方法,除了要与原方法具有相同的简单名称外,还要求必须拥有一个与原方法不同的(不 ...