UVALive - 7041 The Problem to Slow Down You (回文树)
https://vjudge.net/problem/UVALive-7041
题意
给出两个仅包含小写字符的字符串 A 和 B ;
求:对于 A 中的每个回文子串,B 中和该子串相同的子串个数的总和。
分析
从0和1两个根节点DFS下去,如果两个相同的节点同时存在就统计答案。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
//#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
#define pi acos(-1)
//const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + ;
const int maxm = +;
const int mod = ; struct PAM{
int nxt[maxn][];
int fail[maxn];
int cnt[maxn];
int num[maxn];
int len[maxn];
int s[maxn];
int last,n,p; int newnode(int w){
for(int i=;i<;i++) nxt[p][i]=;
num[p]=cnt[p]=;
len[p]=w;
return p++;
}
void init(){
n=last=p=;
newnode();
newnode(-);
s[n]=-;
fail[]=;
}
int get_fail(int x){
while(s[n-len[x]-]!=s[n]) x=fail[x];
return x;
}
void add(int c){
c-='a';
s[++n]=c;
int cur=get_fail(last);
if(!nxt[cur][c]){
int now=newnode(len[cur]+);
fail[now]=nxt[get_fail(fail[cur])][c];
nxt[cur][c]=now;
num[now]=num[fail[now]]+;
}
last=nxt[cur][c];
cnt[last]++;
}
void Count(){
for(int i=p-;i>=;i--) cnt[fail[i]]+=cnt[i];
}
};
PAM pam1,pam2;
char a[maxn],b[maxn];
ll dfs(int an,int bn){
ll res=;
for(int i=;i<;i++)
if(pam1.nxt[an][i]!=&&pam2.nxt[bn][i]!=){
res+=(ll)pam1.cnt[pam1.nxt[an][i]]*pam2.cnt[pam2.nxt[bn][i]]+dfs(pam1.nxt[an][i],pam2.nxt[bn][i]);
}
return res;
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("input.txt", "w", stdout);
#endif
int T,cas=;
scanf("%d",&T); while(T--){
pam1.init();
pam2.init();
scanf("%s%s",a,b);
int l1=strlen(a),l2=strlen(b);
for(int i=;i<l1;i++) pam1.add(a[i]);
for(int i=;i<l2;i++) pam2.add(b[i]);
pam1.Count();
pam2.Count();
ll ans=dfs(,)+dfs(,);
printf("Case #%d: %lld\n",cas++,ans);
}
return ;
}
UVALive - 7041 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 ...
- Codeforces.GYM100548G.The Problem to Slow Down You(回文树)
题目链接 \(Description\) 给定两个串\(S,T\),求两个串有多少对相同回文子串. \(|S|,|T|\leq 2\times 10^5\). \(Solution\) 好菜啊QAQ ...
- UVAlive 7041 The Problem to Slow Down You(回文树)
题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...
- Mediocre String Problem (2018南京M,回文+LCP 3×3=9种做法 %%%千年好题 感谢"Grunt"大佬的细心讲解)
layout: post title: Mediocre String Problem (2018南京M,回文+LCP 3×3=9种做法 %%%千年好题 感谢"Grunt"大佬的细 ...
- UVALive 6933 Virus synthesis(回文树)
Viruses are usually bad for your health. How about ghting them with... other viruses? In this proble ...
- Gym - 101981M:(南京) Mediocre String Problem(回文树+exkmp)
#include<bits/stdc++.h> #define ll long long #define rep(i,a,b) for(int i=a;i<=b;i++) using ...
- 回文自动机 + 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 ...
- The Problem to Slow Down You
The Problem to Slow Down You 输入:t个测试样例,每个样例输入两个字符串 输出:这两对字符串的回文串可以组成多少对本质不同的回文串 题意:给你两个字符串,然后问你这两字符串 ...
- 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 ...
随机推荐
- 【Luogu4396】[AHOI2013]作业(莫队)
[Luogu4396][AHOI2013]作业(莫队) 题面 洛谷 题解 模板题 #include<iostream> #include<cstdio> #include< ...
- 【CF981F】Round Marriage(二分答案,二分图匹配,Hall定理)
[CF981F]Round Marriage(二分答案,二分图匹配,Hall定理) 题面 CF 洛谷 题解 很明显需要二分. 二分之后考虑如果判定是否存在完备匹配,考虑\(Hall\)定理. 那么如果 ...
- 【BZOJ5336】[TJOI2018]party(动态规划)
[BZOJ5336][TJOI2018]party(动态规划) 题面 BZOJ 洛谷 题解 这题好神仙啊... 考虑普通的\(LCS\)的\(dp\),\(f[i][j]=\max\{f[i-1][j ...
- 【BZOJ5213】[ZJOI2018]迷宫(神仙题)
[BZOJ5213][ZJOI2018]迷宫(神仙题) 题面 BZOJ 洛谷 题解 首先可以很容易的得到一个\(K\)个点的答案. 构建\(K\)个点分别表示\(mod\ K\)的余数.那么点\(i\ ...
- IT项目管理——《人月神话》读后感
这也许是和候红老师的最后的几节课了吧,侯老师是一个很有思想深度,很关心同学的好老师. 一开学就布置了阅读<人月神话>的作业,说实话,我没有看,以我的速度可能2.3个小时就看完了,但是我觉得 ...
- 小电阻之大作用——CAN终端电阻
CAN总线终端电阻,顾名思义就是加在总线末端的电阻.此电阻虽小,但在CAN总线通信中却有十分重要的作用. 终端电阻的作用 CAN总线终端电阻的作用有两个: 1.提高抗干扰能力,确保总线快速进入隐性状态 ...
- 2018"百度之星"程序设计大赛 - 资格赛hdu6349三原色(最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6349 题目: 三原色图 Time Limit: 1500/1000 MS (Java/Others) ...
- [THUWC2017]在美妙的数学王国中畅游
[THUWC2017]在美妙的数学王国中畅游 e和sin信息不能直接合并 泰勒展开,大于21次太小,认为是0,保留前21次多项式即可 然后就把e,sin ,kx+b都变成多项式了,pushup合并 上 ...
- zxing二维码的生成与解码(C#)
ZXing是一个开源Java类库用于解析多种格式的1D/2D条形码.目标是能够对QR编码.Data Matrix.UPC的1D条形码进行解码. 其提供了多种平台下的客户端包括:J2ME.J2SE和An ...
- 收藏:win32 控件之 sysLink控件(超链接)
来源:https://blog.csdn.net/dai_jing/article/details/8683487 手动创建syslink(msdn): CreateWindowEx(, WC_LIN ...