UVALive - 7041 G - The Problem to Slow Down You
题意:求两个串的公共回文子串个数
题解:建两个回文自动机,从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的更多相关文章
- 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 ...
- 回文自动机 + 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 ...
- 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 ...
- The Problem to Slow Down You
The Problem to Slow Down You 输入:t个测试样例,每个样例输入两个字符串 输出:这两对字符串的回文串可以组成多少对本质不同的回文串 题意:给你两个字符串,然后问你这两字符串 ...
- UVALive - 7041 The Problem to Slow Down You (回文树)
https://vjudge.net/problem/UVALive-7041 题意 给出两个仅包含小写字符的字符串 A 和 B : 求:对于 A 中的每个回文子串,B 中和该子串相同的子串个数的总和 ...
- UVAlive 7041 The Problem to Slow Down You(回文树)
题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...
- UVALive 6909 Kevin's Problem 数学排列组合
Kevin's Problem 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid ...
- UVALive 7457 Discrete Logarithm Problem (暴力枚举)
Discrete Logarithm Problem 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/D Description ...
- 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 ...
随机推荐
- [转]otunnel:一个和lcx差不多的端口转发的工具
这是一个采用Golang编写的和lcx差不多的端口转发的工具,用来突破内网环境 项目地址 ooclab/otunnel 下载地址(内涵各大平台) http://dl.ooclab.com/otunne ...
- 论文笔记之:DualGAN: Unsupervised Dual Learning for Image-to-Image Translation
DualGAN: Unsupervised Dual Learning for Image-to-Image Translation 2017-06-12 21:29:06 引言部分: 本文提出 ...
- 4-Three-Matterhorn man
What was the main objective of early mountain climbers? ①Modern alpinists try to climb mountains b ...
- Python学习 day04打卡
今天学习的主要内容: 一,列表 1,列表的介绍 列表是python的基础数据类型之一,其他编程语音也有类似的数据类型.例如:JS 中的数组Java中的数组等等. 它是以[]括起来,每个元素用',隔开而 ...
- struct和typedef struct用法和区别
1 首先://注意在C和C++里不同 1.1 在C中定义一个结构体类型要用typedef: typedef struct Student { int a; }Stu; 于是在声明变量的时候就可:Stu ...
- Codeforces 781C Underground Lab
题目链接:http://codeforces.com/problemset/problem/781/C 因为有${K}$个机器人,每个又可以走${\left \lceil \frac{2n}{k} \ ...
- CZK 的饮料店
[题目描述] 一天,小学生 cyx 向你请教了一道他不会做的小学数学题,你瞄了一眼题目,发现题目长下面这样. czk 老板开了个饮料连锁店,连锁店共有 n 家,出售的饮料种类相同. 为了促销,czk ...
- python 排序 由大到小
import functools class Solution: # @param {integer[]} nums # @return {string} def largestNumber(self ...
- git报错fatal: loose object ....(stored in .git/objects/....) is emtpy
主要是非正常关机.把.git给破坏了 参考https://stackoverflow.com/questions/12571557/fixing-a-corrupt-loose-object-as-a ...
- MySQL学习(十四)
utf8的bom问题 在xp下,用记事本创建utf8文件的时候,前面多了3个字节,这3个字节不用来显示,是用来辨识编码用的,EF BB BF告诉记事本,这是utf8编码. 存储引擎和事务简单介绍 引擎 ...