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

  思路:先对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. windows下python文件与文件夹操作

    一.导入模块 imoprt os 二.获取python当前执行的目录 s=os.getcwd() 三.创建文件 import datetime import os dtime=datetime.dat ...

  2. 1009 数字1的数量 数位dp

    1级算法题就这样了,前途渺茫啊... 更新一下博客,我刚刚想套用数位dp的模板,发现用那个模板也是可以做到,而且比第二种方法简单很多 第一种方法:我现在用dp[pos][now]来表示第pos位数字为 ...

  3. TOJ 3973 Maze Again && TOJ 3128 简单版贪吃蛇

    TOJ3973传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=3973 时间限制(普通 ...

  4. XFF的学习+修改源码--Are you in class

    这几天有做天枢CTF的“Are you in class”的题目,虽然以前了解过XFF,但还是没有很好地应用,而且最后居然掉进了一个大坑,且听我细细讲来.   打开题目,首先有个提示“在不在学校主要看 ...

  5. Codeforces Beta Round #49 (Div. 2)

    Codeforces Beta Round #49 (Div. 2) http://codeforces.com/contest/53 A #include<bits/stdc++.h> ...

  6. P3375 【模板】KMP字符串匹配

    P3375 [模板]KMP字符串匹配 https://www.luogu.org/problemnew/show/P3375 题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在 ...

  7. Delphi中记录体做为属性的赋值方法

    1. 起源 此问题源于[秋风人事档案管理系统]用Delphi XE重编译中所发现. 快十年了,当初Delphi 7所编写项目,想用Delphi XE重新编译,并打算做为Free软件发布,编译错误中发现 ...

  8. mybatis批量更新update-设置多个字段值 报错 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near

    mybatis批量更新update-设置多个字段值 2016年08月01日 12:49:26 姚一号 阅读数:29539 标签: mysql mybatis批量更新批量更新allowMultiQuer ...

  9. lombok ------让代码更简洁方便

    估计在平常写代码中,都会创建entity类的实体来,都是那种创建变量,生成set get 方法,方便外部调用,你以为你很流利的操作快捷键就很方便的了? 其实不然,有一个lombok 工具可以帮我们自动 ...

  10. Java_8排序(冒泡排序和选择排序)

    1.选择排序 package demo1; public class Demo1 { public static void main(String[] args) { int[] arr= {23,5 ...