SPOJ Longest Common Substring II
题目连接:戳我
题目大意:求n个字符串的最长公共子串。
它的简化版——这里
当然我们可以用SA写qwq,也可以用广义SAM写qwq
这里介绍纯SAM的写法。。。就是对其中一个建立后缀自动机,然后剩下的N-1个往上面匹配。
设\(sum[i]\)表示到以节点i为根的子树中,最长能够匹配到的最长的子串的长度。
匹配和它的弱化版基本一样,就是注意每次在parent tree上要用拓扑序从下往上更新答案。
注意每个点最大的匹配不能超过它所在类的longest
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define MAXN 1000000
using namespace std;
int T,k,last=1,tot=1;
int a[MAXN],c[MAXN],ans[MAXN],sum[MAXN];
char s[MAXN];
struct Node{int son[26],ff,len;}t[MAXN<<1];
inline void extend(int c)
{
    int p=last,np=++tot;last=np;
    t[np].len=t[p].len+1;
    while(p&&!t[p].son[c])t[p].son[c]=np,p=t[p].ff;
    if(!p)t[np].ff=1;
    else
    {
        int q=t[p].son[c];
        if(t[q].len==t[p].len+1)t[np].ff=q;
        else
        {
            int nq=++tot;
            t[nq]=t[q];
            t[nq].len=t[p].len+1;
            t[q].ff=t[np].ff=nq;
            while(p&&t[p].son[c]==q)t[p].son[c]=nq,p=t[p].ff;
        }
    }
}
int main()
{
	#ifndef ONLINE_JUDGE
	freopen("ce.in","r",stdin);
	#endif
	scanf("%s",s+1);
	int len=strlen(s+1);
	for(int i=1;i<=len;i++) extend(s[i]-'a');
	for(int i=1;i<=tot;i++) c[t[i].len]++;
	for(int i=1;i<=tot;i++) c[i]+=c[i-1];
	for(int i=1;i<=tot;i++) a[c[t[i].len]--]=i;
	for(int i=1;i<=tot;i++) ans[i]=t[i].len;
	while(scanf("%s",s+1)!=EOF)
	{
		len=strlen(s+1);
		memset(sum,0,sizeof(sum));
		int cur_ans=0;
		int now=1;
		for(int i=1;i<=len;i++)
		{
			if(t[now].son[s[i]-'a']) cur_ans++,now=t[now].son[s[i]-'a'];
			else
			{
				while(now&&!t[now].son[s[i]-'a']) now=t[now].ff;
				if(!now) cur_ans=0,now=1;
				else cur_ans=t[now].len+1,now=t[now].son[s[i]-'a'];
			}
			sum[now]=max(sum[now],cur_ans);
		}
		for(int i=tot;i>=1;i--)
		{
			int cur=a[i];
			sum[t[cur].ff]=max(sum[t[cur].ff],min(t[t[cur].ff].len,sum[cur]));
		}
		for(int i=1;i<=tot;i++) ans[i]=min(ans[i],sum[i]);
	}
	int maxx=0;
	for(int i=1;i<=tot;i++) maxx=max(maxx,ans[i]);
	printf("%d\n",maxx);
	return 0;
}
												
											SPOJ Longest Common Substring II的更多相关文章
- 后缀自动机(SAM):SPOJ Longest Common Substring II
		
Longest Common Substring II Time Limit: 2000ms Memory Limit: 262144KB A string is finite sequence of ...
 - 2018.12.15 spoj Longest Common Substring II(后缀自动机)
		
传送门 后缀自动机基础题. 给出10个串求最长公共子串. 我们对其中一个建一个samsamsam,然后用剩下九个去更新范围即可. 代码: #include<bits/stdc++.h> # ...
 - 【SPOJ】Longest Common Substring II (后缀自动机)
		
[SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...
 - spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)
		
spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...
 - 【SPOJ】Longest Common Substring II
		
[SPOJ]Longest Common Substring II 多个字符串求最长公共子串 还是将一个子串建SAM,其他字符串全部跑一边,记录每个点的最大贡献 由于是所有串,要对每个点每个字符串跑完 ...
 - SPOJ LCS2 - Longest Common Substring II
		
LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...
 - 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 ...
 - Longest Common Substring II SPOJ - LCS2 (后缀自动机)
		
Longest Common Substring II \[ Time Limit: 236ms\quad Memory Limit: 1572864 kB \] 题意 给出\(n\)个子串,要求这\ ...
 - spoj1812 LCS2 - Longest Common Substring II
		
地址:http://www.spoj.com/problems/LCS2/ 题面: LCS2 - Longest Common Substring II no tags A string is fi ...
 
随机推荐
- php 使用html5 XHR2 上传文件 进度显示
			
思路:只要我们知道上传文件的总大小,还有上传过程中上传文件的大小,那么就可以实现进度显示了. 在html5中,XMLHttpRequest对象,传送数据的时候,progress事件用来返回进度信息. ...
 - C#中发送邮件,包含Html代码 CDO.Message
			
C#代码: /// <summary> /// 发送邮件 /// </summary> /// <param name="context">&l ...
 - sphinx文档
			
Navigation index modules | Sphinx主页 | 文档 » 下载 目前版本: 1.2 获得 Sphinx 从 Python Package Index, 或者使用如下命令安装 ...
 - 系统调用方式文件编程-open
			
通过Linux系统调用函数编写应用程序,该应用程序实现文件的复制功能 文件描述符--在Linux系统中,所有打开的文件也对应一个数字,这个数字由系统来分配. 1.打开文件--open 头文件:#inc ...
 - 内网IP和公网IP的区别
			
内网IP和公网IP的区别 什么是内网IP: 一些小型企业或者学校,通常都是申请一个固定的IP地址,然后通过IP共享(IP Sharing),使用整个公司或学校的机器都能够访问互联网.而 ...
 - MVC--SSM和SSH简介
 - 2018.09.28 hdu5434 Peace small elephant(状压dp+矩阵快速幂)
			
传送门 看到n的范围的时候吓了一跳,然后发现可以矩阵快速幂优化. 我们用类似于状压dp的方法构造(1(1(1<<m)∗(1m)*(1m)∗(1<<m)m)m)大小的矩阵. 然后 ...
 - 30. Child Labor Problem and Its Solution 童工问题及解决方法
			
30. Child Labor Problem and Its Solution 童工问题及解决方法 ① Over a hundred years ago,Charles Dickens shocke ...
 - Win7 SP1 提示ADO的问题
			
需要安装 Windows6.1-KB2640696-v3-x64.msu 这个Pack
 - c语言学生信息管理系统-学习结构体
			
#include<stdio.h> #include<stdlib.h> //结构体可以存放的学生信息最大个数,不可变变量 ; //学生信息结构体数组,最多可以存放100个学生 ...