题意:求两个串的公共回文子串个数

题解:建两个回文自动机,从0和1各跑一边就是答案了,因为对于回文自动机来说,从头开始dfs就能找出该字符串的所有回文串

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 998244353
#define ld long double
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
//#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
template<typename T>
inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>
inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;} using namespace std; const double eps=1e-8;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=400000+10,maxn=400000+10,inf=0x3f3f3f3f; struct PAM{
int ch[N][26],fail[N],cnt[N],len[N],s[N];
int last,n,p;
int newnode(int w)
{
for(int i=0;i<26;i++)ch[p][i] = 0;
cnt[p] = 0;
len[p] = w;
return p++;
}
void init()
{
p = last = n = 0;
newnode(0);
newnode(-1);
s[n] = -1;
fail[0] = 1;
}
int getfail(int x)
{
while(s[n-len[x]-1] != s[n]) x = fail[x];
return x;
}
void add(int c)
{
s[++n] = c;
int cur = getfail(last);
if(!ch[cur][c]){
int now = newnode(len[cur]+2);
fail[now] = ch[getfail(fail[cur])][c];
ch[cur][c] = now;
}
last = ch[cur][c];
cnt[last]++;
}
void cal()
{
for(int i=p-1;i>=0;i--)
{
cnt[fail[i]]+=cnt[i];
// printf("%d %d\n",i,cnt[i]);
}
// puts("------------");
}
}pam1,pam2;
ll ans;
void dfs(int u1,int u2)
{
if(u1!=0&&u1!=1)ans+=1ll*pam1.cnt[u1]*pam2.cnt[u2];
// printf("%d %d\n",pam1.cnt[u1],pam2.cnt[u2]);
for(int i=0;i<26;i++)
if(pam1.ch[u1][i]&&pam2.ch[u2][i])
dfs(pam1.ch[u1][i],pam2.ch[u2][i]);
}
char s[N];
int main()
{
int T;scanf("%d",&T);
for(int cas=1;cas<=T;cas++)
{
pam1.init();pam2.init();
scanf("%s",s);int n=strlen(s);
for(int i=0;i<n;i++)pam1.add(s[i]-'a');
scanf("%s",s);n=strlen(s);
for(int i=0;i<n;i++)pam2.add(s[i]-'a');
pam1.cal();pam2.cal();
ans=0;dfs(0,0);dfs(1,1);
printf("Case #%d: %lld\n",cas,ans);
}
return 0;
}
/********************
3
abacab
abccab
faultydogeuniversity
hasnopalindromeatall
abbacabbaccab
youmayexpectedstrongsamplesbutnow
********************/

UVALive - 7041 G - The Problem to Slow Down You的更多相关文章

  1. 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 ...

  2. 回文自动机 + 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 ...

  3. CodeForcesGym 100548G The Problem to Slow Down You

    The Problem to Slow Down You Time Limit: 20000ms Memory Limit: 524288KB This problem will be judged ...

  4. The Problem to Slow Down You

    The Problem to Slow Down You 输入:t个测试样例,每个样例输入两个字符串 输出:这两对字符串的回文串可以组成多少对本质不同的回文串 题意:给你两个字符串,然后问你这两字符串 ...

  5. UVALive - 7041 The Problem to Slow Down You (回文树)

    https://vjudge.net/problem/UVALive-7041 题意 给出两个仅包含小写字符的字符串 A 和 B : 求:对于 A 中的每个回文子串,B 中和该子串相同的子串个数的总和 ...

  6. UVAlive 7041 The Problem to Slow Down You(回文树)

    题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...

  7. UVALive 6909 Kevin's Problem 数学排列组合

    Kevin's Problem 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid ...

  8. UVALive 7457 Discrete Logarithm Problem (暴力枚举)

    Discrete Logarithm Problem 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/D Description ...

  9. Gym 101194A / UVALive 7897 - Number Theory Problem - [找规律水题][2016 EC-Final Problem A]

    题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...

随机推荐

  1. Python SSH爆破以及Python3线程池控制线程数

    源自一个朋友的要求,他的要求是只爆破一个ip,结果出来后就停止,如果是爆破多个,完全没必要停止,等他跑完就好 #!usr/bin/env python #!coding=utf-8 __author_ ...

  2. P2221 [HAOI2012]高速公路

    思路 考虑每一条边的贡献,然后推式子 \[ \begin{align}&\sum_{i}V_i\times(R-i+1)\times(i-L+1)\\=&\sum_{i}V_i\lef ...

  3. [蓝桥] 基础练习 数列排序(java)

    问题描述 给定一个长度为n的数列,将这个数列按从小到大的顺序排列.1<=n<=200 输入格式 第一行为一个整数n. 第二行包含n个整数,为待排序的数,每个整数的绝对值小于10000. 输 ...

  4. 【C#】异步的用法

    1. C#5.0 加入了async, await关键字. async是在声明异步方法时使用的修饰符, 声明放在返回值之前即可,await表达式则负责消费异步操作, 不能出现在catch或finally ...

  5. Ambari配置Hive,Hive的使用

    mysql安装,hive环境的搭建 ambari部署hadoop 博客大牛:董的博客 ambari使用 ambari官方文档 hadoop 2.0 详细配置教程 使用Ambari快速部署Hadoop大 ...

  6. Termux 一款安卓终端模拟器

    Termux官网 https://termux.com/ 用处 提供了一个类似于Linux终端的界面,可以使用apt安装程序.目前我在上面跑了我以前写的一些爬虫脚本,运行完全没有问题. 玩法 玩法还是 ...

  7. Vim-编辑器之神

    几点声明: 作者只是一位小小的 \(OIer\) ,并不会什么过于神仙的东西,我这篇文章只是帮助人入门的而已. 若有人在 OI-Wiki上见过了原文章,原作者是我 \(......\) ,真不是抄 \ ...

  8. React Native 组建之IOS和Android通用抽屉

    /** * Sample React Native App * https://github.com/facebook/react-native * @flow *npm:https://www.np ...

  9. 解决github网站打开慢的问题

    一.前言 作为一名合格的程序员,github打开速度太慢怎么能容忍.但是可以通过修改hosts文件信息来解决这个问题.现在chrome访问github速度杠杠的! 二.macOS解决方法 打开host ...

  10. [转][JSBSim]JSBSim的使用--飞行控制组件及其配置

    http://www.jianshu.com/p/b5e9f1f5df95 飞行控制率.稳定增强系统.自动驾驶仪和其他飞控系统(航电.电气等)都能够在 JSBSim 中以独立的控制组件进行建模.JSB ...