题意:给你两个字符串,问你有多少对公共回文串。

  思路:先对a字符串建回文树。然后再把b字符串加进去就好了。

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<cstring>
#include<queue>
#include<set>
#include<string>
#include<map>
#include <time.h>
#define PI acos(-1)
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = +;
const int sigma=;
const ll mod = ;
const int INF = 0x3f3f3f;
const db eps = 1e-;
struct ptree{
char s[maxn];
int next[maxn][sigma], fail[maxn], len[maxn];
ll cntt[maxn], cnt[maxn];
int last, n, p;
ll res;
inline int newnode(int l) {
memset(next[p], , sizeof(next[p]));
cnt[p]=;
cntt[p]=;
len[p]=l;
return p++;
}
inline void init() {
n=, p=, last=;
newnode(), newnode(-);
s[n]=-;
fail[]=;
}
void initt() {
n=, last=;
s[]=-;
fail[]=;
}
inline int FL(int x) {
while(s[n-len[x]-]!=s[n]) x=fail[x];
return x;
}
void add(char c) {
c-='a';
s[++n]=c;
int cur=FL(last);
if (!next[cur][c]) {
int now=newnode(len[cur]+);
fail[now]=next[FL(fail[cur])][c];
next[cur][c]=now;
}
last=next[cur][c];
++cnt[last];
}
inline ll countt() {
for (int i=p-; ~i; --i) {
cnt[fail[i]]+=cnt[i];
}
}
void add1(char c) {
c-='a';
s[++n]=c;
int cur=FL(last);
if (!next[cur][c]) {
int now=newnode(len[cur]+);
fail[now]=next[FL(fail[cur])][c];
next[cur][c]=now;
}
last=next[cur][c];
++cntt[last];
}
inline ll countt1() {
for (int i=p-; ~i; --i) {
cntt[fail[i]]+=cntt[i];
}
}
ll cal() {
//cnt是a字符串的个数,cntt是a相同的回文串的个数
ll pk=;
for (int i=; i<p; i++) {
pk += cnt[i]*cntt[i];
}
return pk;
}
}p;
char a[maxn], b[maxn];
void solve(){
scanf("%s", a);
int len=strlen(a);
scanf("%s", b);
int lenn=strlen(b);
p.init(); //对a建回文树
for (int i=; i<len; i++) {
p.add(a[i]);
}
p.countt();
p.initt(); //再次初始化
for (int i=; i<lenn; i++) {
p.add1(b[i]);
}
p.countt1();
ll ans=p.cal();
static int pa=;
printf("Case #%d: %lld\n", pa++, ans);
}
int main() {
int t = ;
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
scanf("%d", &t);
while(t--) {
solve();
}
return ;
}

UVALive-7041(回文树的更多相关文章

  1. HDU3948 & 回文树模板

    Description: 求本质不同回文子串的个数 Solution: 回文树模板,学一学贴一贴啊... Code: /*================================= # Cre ...

  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. 【CF245H】Queries for Number of Palindromes(回文树)

    [CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...

  4. 【CF17E】Palisection(回文树)

    [CF17E]Palisection(回文树) 题面 洛谷 题解 题意: 求有重叠部分的回文子串对的数量 所谓正难则反 求出所有不重叠的即可 求出以一个位置结束的回文串的数量 和以一个位置为开始的回文 ...

  5. 【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)

    [SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. ...

  6. 【BZOJ2160】拉拉队排练(回文树)

    [BZOJ2160]拉拉队排练(回文树) 题面 BZOJ 题解 看着题目, 直接构建回文树 求出每个回文串的出现次数 直接按照长度\(sort\)一下就行了 然后快速幂算一下答案就出来了 这题貌似可以 ...

  7. 【CF932G】Palindrome Partition(回文树,动态规划)

    [CF932G]Palindrome Partition(回文树,动态规划) 题面 CF 翻译: 给定一个串,把串分为偶数段 假设分为了\(s1,s2,s3....sk\) 求,满足\(s_1=s_k ...

  8. 【BZOJ2342】双倍回文(回文树)

    [BZOJ2342]双倍回文(回文树) 题面 BZOJ 题解 构建出回文树之后 在\(fail\)树上进行\(dp\) 如果一个点代表的回文串长度为\(4\)的倍数 并且存在长度为它的一半的回文后缀 ...

  9. 【BZOJ2565】最长双回文串(回文树)

    [BZOJ2565]最长双回文串(回文树) 题面 BZOJ 题解 枚举断点\(i\) 显然的,我们要求的就是以\(i\)结尾的最长回文后缀的长度 再加上以\(i+1\)开头的最长回文前缀的长度 至于最 ...

  10. [模板] 回文树/回文自动机 && BZOJ3676:[Apio2014]回文串

    回文树/回文自动机 放链接: 回文树或者回文自动机,及相关例题 - F.W.Nietzsche - 博客园 状态数的线性证明 并没有看懂上面的证明,所以自己脑补了一个... 引理: 每一个回文串都是字 ...

随机推荐

  1. background和background-color的区别

    在设置输入框变成一条线的样式时遇到一个小问题. 无论怎么设置 输入框的背景都没有变 而设置background: #aaa;背景就改变了. 后来发现原因 background 可以设置 背景颜色.背景 ...

  2. 通过DataTable获得表的主键

    转载http://www.cnblogs.com/hobe/archive/2005/10/07/249940.html 通过DataTable获得表的主键 很多情形下我们需要知道表的主键是什么.在A ...

  3. unity的inputField文本框赋值问题

    GameObject t = GameObject.Find("InputFieldT"); Text tt = t.transform.Find("Text" ...

  4. UVa 1103 Ancient Messages(二重深搜)

    In order to understand early civilizations, archaeologists often study texts written in ancient lang ...

  5. PAT L2-011 玩转二叉树(二叉树层序遍历)

    给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜面反转,再输出反转后的层序遍历的序列.所谓镜面反转,是指将所有非叶结点的左右孩子对换.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出 ...

  6. Delphi: Class Static Methods

    在Delphi中,自Delphi 2007之后,支持static形式的class方法,样式比如: type TMyClass = class strict private class var FX: ...

  7. nginx配置websocket

    有时候我们需要给websocket服务端做一下nginx的配置,比如需要给websocket服务端做负载均衡,或者,有些系统要求访问websocket的时候不能带端口,这时候我们就需要用nginx来进 ...

  8. 日志排序(gets函数与sscanf的使用)

    题目链接:https://www.nowcoder.com/practice/0f64518fea254c0187ccf0ea05019672?tpId=40&tqId=21363&t ...

  9. python读写剪贴板

    #coding:utf-8 import os import time import win32api import win32con import win32clipboard as w impor ...

  10. iOS 集成极光推送

    最近极光推送更新到V3版本之后,推送又不成功!配合服务器联调了半天,发现是服务器环境配置有问题. 想着就把极光推送的步骤给记录下来. 一.配置push证书 这个可以到极光文档里写,很详细 二.导入必要 ...