多串LCS很适合SA但是我要学SAM

对第一个串求SAM,然后把剩下的串在SAM上跑,也就是维护p和len,到一个点,如果有ch[p][c],就p=ch[p][c],len++,否则向fa找最下的有c[p][c]的p,然后len=dis[p]+1,p=ch[p][c],否则就p=root,len=0(这个len每到一个节点就更新这个节点的f)

然后注意到在parent树上,因为每个节点代表的right集合是儿子的并集,所以f[u]是可以更新f[fa[u]]的,所以从底向上更新一遍(注意先更新!!)

最终点u的f就是每个串的f与dis[u]取min,然后ans在这些点上取max







#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=500005;
int n,m=1,fa[N],ch[N][27],dis[N],cur=1,con=1,la,f[N][15],c[N],a[N],ans;
char s[N];
void ins(int c,int id)
{
la=cur,dis[cur=++con]=id;
int p=la;
for(;p&&!ch[p][c];p=fa[p])
ch[p][c]=cur;
if(!p)
fa[cur]=1;
else
{
int q=ch[p][c];
if(dis[q]==dis[p]+1)
fa[cur]=q;
else
{
int nq=++con;
dis[nq]=dis[p]+1;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
fa[nq]=fa[q];
fa[q]=fa[cur]=nq;
for(;ch[p][c]==q;p=fa[p])
ch[p][c]=nq;
}
}
}
int main()
{
scanf("%s",s+1);
n=strlen(s+1);
for(int i=1;i<=n;i++)
ins(s[i]-'a',i);
for(int i=1;i<=con;i++)
f[i][1]=dis[i];
while(~scanf("%s",s+1))
{
n=strlen(s+1),m++;
for(int i=1,p=1,len=0;i<=n;i++)
{
int c=s[i]-'a';
if(ch[p][c])
len++,p=ch[p][c],f[p][m]=max(f[p][m],len);
else
{
for(;p&&!ch[p][c];p=fa[p]);
if(!p)
p=1,len=0;
else
len=dis[p]+1,p=ch[p][c],f[p][m]=max(f[p][m],len);
}
}
}
for(int i=1;i<=con;i++)
c[dis[i]]++;
for(int i=1;i<=con;i++)
c[i]+=c[i-1];
for(int i=1;i<=con;i++)
a[c[dis[i]]--]=i;
for(int j=2;j<=m;j++)
for(int i=con;i>=1;i--)
f[fa[a[i]]][j]=max(f[fa[a[i]]][j],f[a[i]][j]);
for(int i=1;i<=con;i++)
{
for(int j=2;j<=m;j++)
f[i][1]=min(f[i][1],f[i][j]);
ans=max(ans,f[i][1]);
}
printf("%d\n",ans);
return 0;
}

spoj LCS2 - Longest Common Substring II && LCS - Longest Common Substring【SAM】的更多相关文章

  1. 167. Two Sum II - Input array is sorted【easy】

    167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...

  2. 【SPOJ - LCS2】Longest Common Substring II【SAM】

    题意 求出多个串的最长公共子串. 分析 刚学SAM想做这个题的话最好先去做一下那道codevs3160.求两个串的LCS应该怎么求?把一个串s1建自动机,然后跑另一个串s2,然后找出s2每个前缀的最长 ...

  3. spoj SUBLEX - Lexicographical Substring Search【SAM】

    先求出SAM,然后考虑定义,点u是一个right集合,代表了长为dis[son]+1~dis[u]的串,然后根据有向边转移是添加一个字符,所以可以根据这个预处理出si[u],表示串u后加字符能有几个本 ...

  4. Java for LeetCode 030 Substring with Concatenation of All Words【HARD】

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  5. HDU-1423-Greatest Common Increasing Subsequence-最长公共上升子序列【模版】

    This is a problem from ZOJ 2432.To make it easyer,you just need output the length of the subsequence ...

  6. 167. Two Sum II - Input array is sorted【Easy】【双指针-有序数组求两数之和为目标值的下标】

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  7. spoj NSUBSTR - Substrings【SAM】

    先求个SAM,然后再每个后缀的对应点上标记si[nw]=1,造好SAM之后用吧parent树建出来把si传上去,然后用si[u]更新f[max(u)],最后用j>i的[j]更新f[i] 因为每个 ...

  8. SPOJ LCS2 - Longest Common Substring II 后缀自动机 多个串的LCS

    LCS2 - Longest Common Substring II no tags  A string is finite sequence of characters over a non-emp ...

  9. spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)

    spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...

随机推荐

  1. ng-options bug解决方案(示例)

    情况: 无法获取 ng-model 的值 解决方案: 绑定到对象的属性值上 1.页面 <ion-view hide-nav-bar="true"> <ion-co ...

  2. 亲测linux上安装svn

    方法一: 1.wget http://subversion.tigris.org/downloads/subversion-1.6.1.tar.gz2.wget http://subversion.t ...

  3. C语言语句

    /*Console.Write("你能跑得过豹子吗,请输入 能/不能:"); string a = Console.ReadLine();//接收所输入的字符串内容, if (a= ...

  4. js中创建html标签、加入select下默认的option的value和text、删除select元素节点下全部的OPTION节点

    <pre name="code" class="java"> jsp 中的下拉框标签: <s:select name="sjx&qu ...

  5. Mesa (computer graphics)

    http://en.wikipedia.org/wiki/Mesa_(computer_graphics) Mesa (computer graphics) From Wikipedia, the f ...

  6. Intel的东进与ARM的西征(4)--理想的星空,苹果处理器之野望

    http://www.36kr.com/p/200031.html “人生五十年,如梦亦如幻.有生斯有死,壮士何所憾?”之所以没有遗憾,是因为有了理想. 公元 1582 年,日本战国时期最著名的霸主, ...

  7. 重构机房收费系统你要用的——异常处理和抛出异常(try catch finally)——(vb.net)

    你能保证你的程序不会出问题吗? 不能 当你的程序执行到某个地方发生了你不想要的结果.你是否想让它一错再错? 不想 你是否想让你的程序占着茅坑不拉屎? 不想 你是否想知道你的程序出错的原因? 想 个问题 ...

  8. no matching provisioning profiles found

    问题:真机连上,执行这个提示. 解决: 项目->targets->Bulid Settings-> 1,Provisioning Profile->选择配置Bundle Ide ...

  9. DoubleViewPager

    https://github.com/eltld/DoubleViewPager https://github.com/eltld/DoubleViewPagerSample

  10. Why there are no job running on hadoop

    Using hadoop1.3.0. I ran the example WordCount correctly in eclipse. But when I enter localhost:5003 ...