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

题解:建两个回文自动机,从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. MongoDB集群配置笔记二(实战)

    单台mongodb配置文件: dbpath=/opt/mongodb/data logpath=/opt/mongodb/logs/mongodb.log logappend=true fork=tr ...

  2. bootstrap图片上传功能

    重点: fileupload    .loadImage 引用js: <!-- Bootstrap CSS --> <link href="~/lib/bootstrap/ ...

  3. AmazeUI学习

    http://amazeui.org/ 相比于其他国外的框架而言,Amaze UI更关注中文排版,被前端工程师称为最懂中文的前端框架. Amaze UI受欢迎的一个重要的原因是:文档非常完善,适合各阶 ...

  4. Linux let 命令

    命令:let let 命令是 BASH 中用于计算的工具,用于执行一个或多个表达式,变量计算中不需要加上 $ 来表示变量.如果表达式中包含了空格或其他特殊字符,则必须引起来. 语法格式 let arg ...

  5. burp suite 的intruder 四种攻击方式

    一:sniper[狙击手] 这种攻击基于原始的请求内容,需要一个字典,每次用字典里的一个值去代替一个待攻击的原始值. 攻击次数=参数个数X字典内元素个数 例如:原始请求中 name=aa , pass ...

  6. Python3 函数注解

    Python3提供一种语法,用于为函数声明中的参数和返回值附加元数据.下面的例子是注解后的版本,特点在第一行: 1 def clip(text : str, max_len : 'int > 0 ...

  7. 【C#】 基于ArcFace 2.0—视频人脸识别Demo

    使用的虹软人脸识别技术 啥话不说,不用跪求,直接给下载地址:http://common.tenzont.com/comdll/arcface2demo.zip(话说附件的大小不限制,还是说我的文件太大 ...

  8. CSS段落对齐方式

    CSS段落对齐有两种方式:水平对齐和垂直对齐. 1.水平对齐: (1).text-align:left;         //左对齐 (2).text-align:right;      //右对齐 ...

  9. Fiddler 简单介绍

    fiddler 也已经使用了几年了,前面做免登录时就是用了fiddler,为了抓取cookie等信息.但是一直没有对他进行整理出一篇文章来介绍其使用. Fiddler的基本介绍 Fiddler的官方网 ...

  10. alfred

    1.alfred怎么设置默认的搜索项. https://www.zhihu.com/question/20205127 2.