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 ...
随机推荐
- centos7安装部署本地局域网yum源
应用场景: 当Linux系统都是最小化安装的系统,又无法做到每台都能访问外网的情况下,安装常用工具或者依赖包的最好办法可能就是建立本地yum源了. 安装环境: 一台 centos 7.4 minima ...
- iptables防火墙详解(一)
-- 防火墙 常见的防火墙 :瑞星 江民 诺顿 卡巴斯基 天网...... iptables firewalld http://www.netfilter.org/ netfilter / iptab ...
- 随机总数字里面选取随机数字进行随机排序案例(JAVA实现)
随机总数字里面选取随机数字进行随机排序案例,案例如下: 代码code: package com.sec; import java.util.Arrays; import java.util.Scann ...
- Spring的核心
技术书籍这么多,每次好不容易读完一本,但总过不了多久就会遗忘.为了对抗,整理记录和回看,也是实属必要.由此,从这<Spring 实战(第四版)>开始,记录一下知识点,下次再要复习时,能免去 ...
- docker file 示例
报错 Cannot connect to the Docker daemon. Is the docker daemon running on this host? 这个错误只要输入docker -d ...
- MVC aspx
客户端服务器---Model(模型)---View(视图)---Control(控制器) 1.ASP.NET MVC 2.新建项目引擎选aspx.在Controllers创建控制器,默认启动Home ...
- P1966 火柴排队
这道题需要小小的思考一波 (然而我思考了两节课) 好,我们先得出一个结论:a中第k大的与b中第k大的一定要排在一起,才能保证最小. 然后发现:挪a,b其实没有区别,故我们固定a,挪b. 然后我们就思考 ...
- UOJ#422 小Z的礼物
非常神奇的一个套路......首先min-max容斥一波,变成枚举子集然后求所有子集min的期望. 一个子集的期望怎么求?我们可以求出所有的r个选法中能够选到这个子集的方案数k,那么概率就是k / r ...
- [bzoj1692][队列变换]
题目链接 思路 首先肯定想到贪心,从队尾和队首取更小的那个. 但是如果遇到队尾和队首一样大的情况呢,总不能再挨个往前比较.所以就把整个字符串倒过来再挂到现在字符串的后面,也就是把当前字符串对称过去.然 ...
- T4 反射实体模型生成代码(Demo)
1.新建一个T4 Script <#@ template language="C#" debug="True" #> <#@ output ...