Problem Description
MZL's Circle Zhou is good at solving some counting problems. One day, he comes up with a counting problem:
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.
 
Input
The first line of the input is a single integer T (T≤5), indicating the number of testcases. 
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.
 
Output
For each test case, output one line, a single integer indicating the answer.
 
Sample Input
2
acbcc
cccabc
bbbabbababbababbaaaabbbbabbaaaabaabbabbabbbaaabaab
abbaabbabbaaaabbbaababbabbabababaaaaabbaabbaabbaab
 
Sample Output
135
557539
 
Author
SXYZ
 
Source
2015 Multi-University Training Contest 5
 
题解:题目意思给你两个字符串,然后从第一个字符串里面取出一个子串X,从第二个字符串里面取出一个子串Y,两个拼接在一起组成新的字符串,其中X、Y都可以是空串,问有多少个这样不同的串。
思路:考虑X+Y为什么会有重复的。举个例子ababc,可以由ab+abc或则a+babc组成。重复的原因在于在第一个串中可以找到ab,也可以找到a,而如果a可以构成这个拼接串,那么ab也构成这个拼接串。所以说,为了避免重复,我们可以在第一个串中找最长的点,即走到某个点x,然后这个点不能走到字符'a',那么对于字符'a'来说,x这个点就是最长的,在另一个串中找'a'开头的子串个数,这些就是点x的可以匹配到的个数。(用记忆化搜索)

 

参考代码:
#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+记忆化搜索)的更多相关文章

  1. HDU5343:MZL's Circle Zhou(SAM,记忆化搜索DP)

    Description Input Output Sample Input Sample Output Solution 题意:给你两个串,分别从两个里面各选出一个子串拼到一起,问能构成多少个本质不同 ...

  2. 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形成的不 ...

  3. [HDU5343]MZL's Circle Zhou

    题目大意: 给你两个字符串a和b,从中分别取出子串x和y,求不同的x+y的个数. 思路: 对于每一个字符串,构建SAM. 为了保证相同的x+y不会被重复统计,我们可以想办法只统计相同的x+y中x最长的 ...

  4. 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 ...

  5. HDU 5343 MZL's Circle Zhou

    MZL's Circle Zhou Time Limit: 1000ms Memory Limit: 131072KB This problem will be judged on HDU. Orig ...

  6. Codeforces Round #406 (Div. 1) A. Berzerk 记忆化搜索

    A. Berzerk 题目连接: http://codeforces.com/contest/786/problem/A Description Rick and Morty are playing ...

  7. HDU 1208 Pascal's Travels 经典 跳格子的方案数 (dp或者记忆化搜索)

    Pascal's Travels Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Su ...

  8. POJ 2704 Pascal's Travels 【DFS记忆化搜索】

    题目传送门:http://poj.org/problem?id=2704 Pascal's Travels Time Limit: 1000MS   Memory Limit: 65536K Tota ...

  9. [ACM_动态规划] 数字三角形(数塔)_递推_记忆化搜索

    1.直接用递归函数计算状态转移方程,效率十分低下,可以考虑用递推方法,其实就是“正着推导,逆着计算” #include<iostream> #include<algorithm> ...

随机推荐

  1. php ffmpeg截取视频第一帧保存为图片的方法

    php ffmpeg截取视频第一帧保存为图片的方法 <pre> $xiangmupath = $this->getxiangmupath(); $filename = 'chengs ...

  2. 130道ASP.NET面试题(一)

    1 .简述 private,protected,public,internal修饰符的访问权限 答: private : 私有成员, 在类的内部才可以访问. protected : 保护成员,该类内部 ...

  3. C#动态多态性的理解

    C#动态多态性是通过抽象类和虚方法实现的. 抽象类的理解 用关键字abstract创建抽象类,用于提供接口的部分类的实现(理解:接口不能提供实现,抽象类中可以有实现,接口与抽象类一起使用,可以达到父类 ...

  4. PHP 格式化公钥私钥(pem文件)

    <?php header("Content-Type: text/html; charset=utf-8"); $filename = dirname(__FILE__).& ...

  5. jquery ajax在 IE8/IE9 中无效

    你们是不是也曾经和我以为遇到过这样的情况呢,jquery ajax在 IE8/IE9 中无效获取不到数据呢,经过熬夜找到好的东西和你们分享一下就是jQuery-ajaxTransport-XDomai ...

  6. P0-Logisim简单部件与有限状态机

    #自学了6week,pre都挂了,做了做P0课下测试,觉得自己对有限状态机概念的的理解,特别是牵扯到时序还是很模糊:状态的抽象也不够熟练:logisim和Verilog的实现也存在问题.网上针对性的l ...

  7. eNSP仿真软件之配置Trunk口

    实验原理 在以太网中,通过划分VLAN来隔离广播域和增强网络通信的安全性.以太网通常由多台交换机组成,为了使VLAN的数据帧跨越多台交换机传递,交换机之间互连的链路需要配置为干道链路(Trunk Li ...

  8. PL真有意思(五):数据类型

    前言 现在大多数程序设计语言中都有表达式和/或对象的类型概念.类型起着两种主要作用: 为许多操作提供了隐含的上下文信息,使程序员可以在许多情况下不必显示的描述这种上下文.比如int类型的两个对象相加就 ...

  9. 来理解undefined 和 null 区别

    之前虽然也知道这两个之间的区别,但是让我描述的话,感觉上还是说的不是很清楚.今天也详细看了一次这个知识点,现在来说说这两者间的区别. null: Null类型,代表“空值”,代表一个空对象指针,使用t ...

  10. API规范约定

    为了高效开发,节约编写文档的成本,API服务使用Swagger来描述 一.API设计原则 控制API的粒度和数量 命名要遵循简单.可读.统一原则: 优先设计API,然后编码 二.URL设计[针对后端开 ...