HDU5343 MZL's Circle Zhou(SAM+记忆化搜索)
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.
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.
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+10; char s1[maxn],s2[maxn]; struct SAM{ int last,tot,nxt[maxn<<1][27],fa[maxn<<1],l[maxn<<1]; inline void Init() { last=tot=1; memset(nxt[tot],0,sizeof(nxt[tot])); l[tot]=fa[tot]=0; } inline int NewNode() { ++tot; memset(nxt[tot],0,sizeof(nxt[tot])); l[tot]=fa[tot]=0; return tot; } inline void Add(int c) { int np=NewNode(),p=last; last=np;l[np]=l[p]+1; while(p&&!nxt[p][c]) nxt[p][c]=np,p=fa[p]; if(!p) fa[np]=1; else { int q=nxt[p][c]; if(l[q]==l[p]+1) fa[np]=q; else { int nq=NewNode(); memcpy(nxt[nq],nxt[q],sizeof(nxt[q])); fa[nq]=fa[q]; l[nq]=l[p]+1; 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<<1],dp2[maxn<<1]; inline ull dfs2(int u) { if(!u) return 0; if(dp2[u]) return dp2[u]; ull res=1; for(int i=0;i<26;++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=1; for(int i=0;i<26;++i) { int nt=sam1.nxt[u][i]; if(nt) res+=dfs(nt); else res+=dfs2(sam2.nxt[1][i]); } return dp1[u]=res; } int main() { scanf("%d",&T); while(T--) { sam1.Init();sam2.Init(); memset(dp1,0,sizeof dp1); memset(dp2,0,sizeof dp2); scanf("%s%s",s1,s2); for(int i=0;s1[i];++i) sam1.Add(s1[i]-'a'); for(int i=0;s2[i];++i) sam2.Add(s2[i]-'a'); printf("%I64u\n",dfs(1)); } return 0; }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· dotnet 源代码生成器分析器入门
· 官方的 MCP C# SDK:csharp-sdk
· 一款 .NET 开源、功能强大的远程连接管理工具,支持 RDP、VNC、SSH 等多种主流协议!
· 一步一步教你部署ktransformers,大内存单显卡用上Deepseek-R1
· 一次Java后端服务间歇性响应慢的问题排查记录