HDU5343 MZL's Circle Zhou(SAM+记忆化搜索)
You are given two strings a,b which consist of only lowercase English letters. You can subtract a substring x (maybe empty) from string a and a substring y (also maybe empty) from string b, and then connect them as x+y with x at the front and y at the back. In this way, a series of new strings can be obtained.
The question is how many different new strings can be obtained in this way.
Two strings are different, if and only if they have different lengths or there exists an integer i such that the two strings have different characters at position i.
For each test case, there are two lines, the first line is string a, and the second line is string b. 1<=|a|,|b|<=90000.
acbcc
cccabc
bbbabbababbababbaaaabbbbabbaaaabaabbabbabbbaaabaab
abbaabbabbaaaabbbaababbabbabababaaaaabbaabbaabbaab
557539
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define RI register int
const int maxn=1e5+;
char s1[maxn],s2[maxn];
struct SAM{
int last,tot,nxt[maxn<<][],fa[maxn<<],l[maxn<<];
inline void Init()
{
last=tot=;
memset(nxt[tot],,sizeof(nxt[tot]));
l[tot]=fa[tot]=;
}
inline int NewNode()
{
++tot;
memset(nxt[tot],,sizeof(nxt[tot]));
l[tot]=fa[tot]=;
return tot;
}
inline void Add(int c)
{
int np=NewNode(),p=last;
last=np;l[np]=l[p]+;
while(p&&!nxt[p][c]) nxt[p][c]=np,p=fa[p];
if(!p) fa[np]=;
else
{
int q=nxt[p][c];
if(l[q]==l[p]+) fa[np]=q;
else
{
int nq=NewNode();
memcpy(nxt[nq],nxt[q],sizeof(nxt[q]));
fa[nq]=fa[q];
l[nq]=l[p]+;
fa[q]=fa[np]=nq;
while(p&&nxt[p][c]==q) nxt[p][c]=nq,p=fa[p];
}
}
}
} sam1,sam2; int T;
ull dp1[maxn<<],dp2[maxn<<];
inline ull dfs2(int u)
{
if(!u) return ;
if(dp2[u]) return dp2[u];
ull res=;
for(int i=;i<;++i)
{
int nt=sam2.nxt[u][i];
if(nt) res+=dfs2(nt);
}
return dp2[u]=res;
}
inline ull dfs(int u)
{
if(dp1[u]) return dp1[u];
ull res=;
for(int i=;i<;++i)
{
int nt=sam1.nxt[u][i];
if(nt) res+=dfs(nt);
else res+=dfs2(sam2.nxt[][i]);
}
return dp1[u]=res;
} int main()
{
scanf("%d",&T);
while(T--)
{
sam1.Init();sam2.Init();
memset(dp1,,sizeof dp1);
memset(dp2,,sizeof dp2); scanf("%s%s",s1,s2);
for(int i=;s1[i];++i) sam1.Add(s1[i]-'a');
for(int i=;s2[i];++i) sam2.Add(s2[i]-'a'); printf("%I64u\n",dfs());
} return ;
}
HDU5343 MZL's Circle Zhou(SAM+记忆化搜索)的更多相关文章
- HDU5343:MZL's Circle Zhou(SAM,记忆化搜索DP)
Description Input Output Sample Input Sample Output Solution 题意:给你两个串,分别从两个里面各选出一个子串拼到一起,问能构成多少个本质不同 ...
- hdu 5343 MZL's Circle Zhou SAM
MZL's Circle Zhou 题意:给定两个长度不超过a,b(1 <= |a|,|b| <= 90000),x为a的连续子串,b为y的连续子串(x和y均可以是空串):问x+y形成的不 ...
- [HDU5343]MZL's Circle Zhou
题目大意: 给你两个字符串a和b,从中分别取出子串x和y,求不同的x+y的个数. 思路: 对于每一个字符串,构建SAM. 为了保证相同的x+y不会被重复统计,我们可以想办法只统计相同的x+y中x最长的 ...
- HDU 5343 MZL's Circle Zhou 后缀自动机+DP
MZL's Circle Zhou Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- HDU 5343 MZL's Circle Zhou
MZL's Circle Zhou Time Limit: 1000ms Memory Limit: 131072KB This problem will be judged on HDU. Orig ...
- Codeforces Round #406 (Div. 1) A. Berzerk 记忆化搜索
A. Berzerk 题目连接: http://codeforces.com/contest/786/problem/A Description Rick and Morty are playing ...
- HDU 1208 Pascal's Travels 经典 跳格子的方案数 (dp或者记忆化搜索)
Pascal's Travels Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Su ...
- POJ 2704 Pascal's Travels 【DFS记忆化搜索】
题目传送门:http://poj.org/problem?id=2704 Pascal's Travels Time Limit: 1000MS Memory Limit: 65536K Tota ...
- [ACM_动态规划] 数字三角形(数塔)_递推_记忆化搜索
1.直接用递归函数计算状态转移方程,效率十分低下,可以考虑用递推方法,其实就是“正着推导,逆着计算” #include<iostream> #include<algorithm> ...
随机推荐
- php ffmpeg截取视频第一帧保存为图片的方法
php ffmpeg截取视频第一帧保存为图片的方法 <pre> $xiangmupath = $this->getxiangmupath(); $filename = 'chengs ...
- 130道ASP.NET面试题(一)
1 .简述 private,protected,public,internal修饰符的访问权限 答: private : 私有成员, 在类的内部才可以访问. protected : 保护成员,该类内部 ...
- C#动态多态性的理解
C#动态多态性是通过抽象类和虚方法实现的. 抽象类的理解 用关键字abstract创建抽象类,用于提供接口的部分类的实现(理解:接口不能提供实现,抽象类中可以有实现,接口与抽象类一起使用,可以达到父类 ...
- PHP 格式化公钥私钥(pem文件)
<?php header("Content-Type: text/html; charset=utf-8"); $filename = dirname(__FILE__).& ...
- jquery ajax在 IE8/IE9 中无效
你们是不是也曾经和我以为遇到过这样的情况呢,jquery ajax在 IE8/IE9 中无效获取不到数据呢,经过熬夜找到好的东西和你们分享一下就是jQuery-ajaxTransport-XDomai ...
- P0-Logisim简单部件与有限状态机
#自学了6week,pre都挂了,做了做P0课下测试,觉得自己对有限状态机概念的的理解,特别是牵扯到时序还是很模糊:状态的抽象也不够熟练:logisim和Verilog的实现也存在问题.网上针对性的l ...
- eNSP仿真软件之配置Trunk口
实验原理 在以太网中,通过划分VLAN来隔离广播域和增强网络通信的安全性.以太网通常由多台交换机组成,为了使VLAN的数据帧跨越多台交换机传递,交换机之间互连的链路需要配置为干道链路(Trunk Li ...
- PL真有意思(五):数据类型
前言 现在大多数程序设计语言中都有表达式和/或对象的类型概念.类型起着两种主要作用: 为许多操作提供了隐含的上下文信息,使程序员可以在许多情况下不必显示的描述这种上下文.比如int类型的两个对象相加就 ...
- 来理解undefined 和 null 区别
之前虽然也知道这两个之间的区别,但是让我描述的话,感觉上还是说的不是很清楚.今天也详细看了一次这个知识点,现在来说说这两者间的区别. null: Null类型,代表“空值”,代表一个空对象指针,使用t ...
- API规范约定
为了高效开发,节约编写文档的成本,API服务使用Swagger来描述 一.API设计原则 控制API的粒度和数量 命名要遵循简单.可读.统一原则: 优先设计API,然后编码 二.URL设计[针对后端开 ...