【题意】给定长度为n和m的两个字符串S和T,要求在字符串S中取出若干段拼成T(可重复取),求最小段数,n,m<=50000。

【算法】后缀自动机 || 后缀数组

【题解】对串S建SAM,然后在上面尽可能地匹配T,匹配几次得到T就是答案。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
using namespace std;
const int maxn=;
struct tree{int len,fa,t[];}t[maxn*];
int tot,last,n,m,b[maxn];
void insert(int c){
int np=++tot;
t[np].len=t[last].len+;
int x=last;
while(x&&!t[x].t[c])t[x].t[c]=np,x=t[x].fa;
last=np;
if(!x)t[np].fa=;else{
int y=t[x].t[c];
if(t[y].len==t[x].len+)t[np].fa=y;else{
int nq=++tot;
t[nq]=t[y];
t[nq].len=t[x].len+;
t[nq].fa=t[y].fa;t[y].fa=t[np].fa=nq;
while(x&&t[x].t[c]==y)t[x].t[c]=nq,x=t[x].fa;
}
}
}
int main(){
scanf("%d%d",&n,&m);
tot=last=;
for(int i=;i<=n;i++){
char c=getchar();
while(c<'A'||c>'Z')c=getchar();
insert(c-'A');
}
for(int i=;i<=m;i++){
char c=getchar();
while(c<'A'||c>'Z')c=getchar();
b[i]=c-'A';
}
int len=,ans=;
while(len<=m){
int now=;
while(len<=m&&t[now].t[b[len]])now=t[now].t[b[len++]];
ans++;
}
printf("%d",ans);
return ;
}

最新文章

  1. 《寒江独钓_Windows内核安全编程》中修改类驱动分发函数
  2. Ajax在html页面获取后台XML文件资源
  3. Visual Studio 2012 cannot identify IHttpActionResult
  4. BZOJ3631[JLOI2014]松鼠的新家 题解
  5. Linux:Linux 重要人物
  6. iOS8 VPN 应用内连接
  7. Linq---左外联查询
  8. ZOJ-2338 The Towers of Hanoi Revisited 输出汉诺塔的最优解移动过程
  9. 3.3 使用Code First数据库迁移
  10. DEEP LEARNING IS THE FUTURE: Q&amp;A WITH NAVEEN RAO OF NERVANA SYSTEMS
  11. 如何让MFC程序关闭按钮失效,也无法右击任务栏关闭窗口来关闭?
  12. linux核心之进程管理
  13. Android使用学习之画图(Canvas,Paint)与手势感应及其应用(乒乓球小游戏)
  14. hdu1312 Red and Black 简单BFS
  15. ISP(Interface Segregation Principle),接口隔离原则
  16. 《java入门第一季》之好玩的正则表达式
  17. Java基础知识回顾之五 ----- 多线程
  18. dataguard主库删除归档日志后从库恢复的方法
  19. js立即执行函数用法
  20. MVC aspx

热门文章

  1. lintcode-414-两个整数相除
  2. Spring的初始化:org.springframework.web.context.ContextLoaderListener
  3. erlang随机排列数组
  4. NFS 它的目的就是想让不同的机器、不同的作业系统可以彼此分享个别的档案啦
  5. LoadRunner函数大全之中文解释
  6. 第194天:js---函数对象详解(arguments、length)
  7. 堆模板(pascal)洛谷P3378
  8. BZOJ3566 SHOI2014概率充电器(动态规划+概率期望)
  9. spring的事务传播特性
  10. 【LOJ6436】【PKUSC2018】神仙的游戏(NTT)