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

  思路:先对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. ffmpeg编码中的二阻塞一延迟

    1. avformat_find_stream_info接口延迟 不论是减少预读的数据量,还是设置flag不写缓存,我这边都不实用,前者有风险,后者会丢帧,可能我还没找到好姿势,记录在此,参考:htt ...

  2. 201. Bitwise AND of Numbers Range (Bit)

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND(按位与) of all nu ...

  3. Appium+python自动化3-定位元素

    3.1常用定位方法讲解 对象定位是自动化测试中很关键的一步,也可以说是最关键的一步,毕竟你对象都没定位那么你想操作也不行.所以本章节的知识我希望大家多动手去操作,不要仅仅只是书本上的知识,毕竟这个我只 ...

  4. manipulate

    manipulate - 必应词典 美[mə'nɪpjə.leɪt]英[mə'nɪpjʊleɪt] v.控制:摆布:(有技巧地)使用:巧妙地处理(问题等) 网络操纵:被操纵:被控体 变形第三人称单数: ...

  5. 《纪念碑谷》(Monument Valley) 系列游戏的空间结构是如何设计的?

    这个游戏有着像埃舍尔的画一样有着空间结构的矛盾,如果是2D游戏,艾达可以行走的路线.建筑结构变化的过程是如何绘制的?如果是3D游戏,那么如何对这种违背物理定律的矛盾空间进行建模?以前有一个叫“无限回廊 ...

  6. express 学习札记

    Enjoy yourself! 祝你玩得开心! I have no idea. 我没有头绪. I just made it! 我做到了!  I’ll see to it 我会留意的. Express ...

  7. js常用的数组,,字符串,,Math..正则方法

    数组 slice[start,end] 返回从原数组中指定开始下标到结束下标之间的项目组成新数组(不会影响原数组) splice() 1.删除功能:2个参数 , 起始位置 , 删除的项目 2.插入功能 ...

  8. 使用go语言的list实现一个简单的LRU缓存

    package main; import ( "container/list" "errors" "sync" "fmt" ...

  9. win下svn常用操作笔记

    svn基本命令 checkout 检出 把服务器代码下载到本地一份update 更新 把服务器上的最新代码更新到本地commit 提交 把本地代码提交到服务器上 win下svn的客户端工具Tortoi ...

  10. 定时器中的this和函数封装的简单理解;

    一.定时器中的this: 不管定时器中的函数怎么写,它里面的this都是window: 在函数前面讲this赋值给一个变量,函数内使用这个变量就可以改变this的指向 二.函数封装 函数封装是一种函数 ...