传送门

好像用SAM写的很少诶……

其实我一开始也没想到要用SAM的……主要是没有想到找的时候可以dfs……

首先建一个SAM,然后跑一遍dfs,枚举一下下一位,如果相同直接继续,否则就花费一次次数来改变它,保证改变次数小于等于3就行了

 // luogu-judger-enable-o2
//minamoto
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=1e6+;
int cnt[N<<],fa[N<<],ch[N<<][],l[N<<],last,tot;
char s[N];int c[N],a[N],ans,m,val[N];
void ins(int c){
int p=last,np=++tot;last=np,l[np]=l[p]+,cnt[np]=;
for(;p&&!ch[p][c];p=fa[p]) ch[p][c]=np;
if(!p) fa[np]=;
else{
int q=ch[p][c];
if(l[q]==l[p]+) fa[np]=q;
else{
int nq=++tot;l[nq]=l[p]+;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
fa[nq]=fa[q],fa[q]=fa[np]=nq;
for(;ch[p][c]==q;p=fa[p]) ch[p][c]=nq;
}
}
}
inline void calc(){
memset(c,,sizeof(c));
for(int i=;i<=tot;++i) ++c[l[i]];
for(int i=;i<=tot;++i) c[i]+=c[i-];
for(int i=;i<=tot;++i) a[c[l[i]]--]=i;
for(int i=tot,p;i;--i)
p=a[i],cnt[fa[p]]+=cnt[p];
}
void dfs(int p,int len,int j){
if(len>m) return (void)(ans+=cnt[p]);
for(int i=;i<;++i){
if(!ch[p][i]) continue;
// val[s[len]]==i?dfs(ch[p][i],len+1,j):j<3?dfs(ch[p][i],len+1,j+1):(void)(1);
if(val[s[len]]==i) dfs(ch[p][i],len+,j);
else if(j<) dfs(ch[p][i],len+,j+);
}
}
int main(){
// freopen("testdata.in","r",stdin);
val['T']=,val['A']=,val['C']=,val['G']=;
int T;scanf("%d",&T);
while(T--){
memset(ch,,sizeof(ch)),memset(cnt,,sizeof(cnt));
last=tot=,ans=;
scanf("%s",s+);int len=strlen(s+);
for(int i=;i<=len;++i) ins(val[s[i]]);
calc();
scanf("%s",s+),m=strlen(s+);
dfs(,,);printf("%d\n",ans);
}
return ;
}

洛谷P3763 [TJOI2017]DNA(后缀自动机)的更多相关文章

  1. 洛谷P3763 [TJOI2017]DNA(后缀数组 RMQ)

    题意 题目链接 Sol 这题打死我也不会想到后缀数组的,应该会全程想AC自动机之类的吧 但知道这题能用后缀数组做之后应该就不是那么难了 首先把\(S\)和\(S0\)拼到一起跑,求出Height数组 ...

  2. 洛谷P3763 [Tjoi2017]DNA 【后缀数组】

    题目链接 洛谷P3763 题解 后缀数组裸题 在BZOJ被卡常到哭QAQ #include<algorithm> #include<iostream> #include< ...

  3. [洛谷P3763] [TJOI2017]DNA

    洛谷题目链接:[TJOI2017]DNA 题目描述 加里敦大学的生物研究所,发现了决定人喜不喜欢吃藕的基因序列S,有这个序列的碱基序列就会表现出喜欢吃藕的性状,但是研究人员发现对碱基序列S,任意修改其 ...

  4. 洛谷P4248 [AHOI2013]差异(后缀自动机求lcp之和)

    题目见此 题解:首先所有后缀都在最后一个np节点,然后他们都是从1号点出发沿一些字符边到达这个点的,所以下文称1号点为根节点,我们思考一下什么时候会产生lcp,显然是当他们从根节点开始一直跳相同节点的 ...

  5. BZOJ.4892.[TJOI2017]DNA(后缀自动机/后缀数组)

    题目链接 \(Description\) 给出两个串\(S,T\),求\(T\)在\(S\)中出现了多少次.出现是指.可以有\(3\)次(\(3\)个字符)不匹配(修改使其匹配). \(Solutio ...

  6. [TJOI2017]DNA --- 后缀数组

    [TJOI2017]DNA 题目描述 加里敦大学的生物研究所,发现了决定人喜不喜欢吃藕的基因序列S, 有这个序列的碱基序列就会表现出喜欢吃藕的性状,但是研究人员发现对碱基序列S,任意修改其中不超过3个 ...

  7. [洛谷P3761] [TJOI2017]城市

    洛谷题目链接:[TJOI2017]城市 题目描述 从加里敦大学城市规划专业毕业的小明来到了一个地区城市规划局工作.这个地区一共有ri座城市,<-1条高速公路,保证了任意两运城市之间都可以通过高速 ...

  8. [TJOI2017] DNA - 后缀数组,稀疏表

    [TJOI2017] DNA Description 求模式串与主串的匹配次数,容错不超过三个字符. Solution 枚举每个开始位置,进行暴力匹配,直到失配次数用光或者匹配成功.考虑到容错量很小, ...

  9. 洛谷-P5357-【模板】AC自动机(二次加强版)

    题目传送门 -------------------------------------- 过年在家无聊补一下这周做的几道AC自动机的模板题 sol:AC自动机,还是要解决跳fail边产生的重复访问,但 ...

随机推荐

  1. Java for LeetCode 132 Palindrome Partitioning II

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  2. bind、call、apply的区别与实现原理

    1.简单说一下bind.call.apply的区别 三者都是用于改变函数体内this的指向,但是bind与apply和call的最大的区别是:bind不会立即调用,而是返回一个新函数,称为绑定函数,其 ...

  3. web tools for sublime

    Your code editor is your main development tool; you use it to write and save lines of code. Write be ...

  4. Adding Form Fields to a MS Word Document

    Configuring a Word Merge in SmartSimple is a three-step process: Create the MS Word document that wi ...

  5. html5--1.8超链接下

    html5--1.8超链接下 下面演示链接打开新网友不关闭原网页. 外部网站: 百度 这是用a标签的target属性实现的,用的target="_blank" 这样新出现的页面会另 ...

  6. Linux_异常_03_Failed to restart iptables.service: Unit not found.

    启动防火墙时出现: Failed to restart iptables.service: Unit not found. 解决方案: 1.https://stackoverflow.com/ques ...

  7. leetcode 191 Number of 1 Bits(位运算)

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...

  8. CodeForces - 1005E2:Median on Segments (General Case Edition) (函数的思想)

    You are given an integer sequence a1,a2,…,ana1,a2,…,an. Find the number of pairs of indices (l,r)(l, ...

  9. 使用django-extension扩展django的manage――runscript命令

    摘要:1.下载安装   1)$easy_installdjango-extensions   2)在INSTALLED_APP中添加'django_extensions'[python]INSTALL ...

  10. poj2228Naptime——环形DP

    题目:http://poj.org/problem?id=2228 dp[i][j][0/1]表示前i小时中第j小时睡(1)或不睡(0)的最优值: 注意第一个小时,若睡则对最终取结果有要求,即第n个小 ...