传送门:http://begin.lydsy.com/JudgeOnline/problem.php?id=2797

题目大意:给你几个串求出几个串中的最长公共子串。

题解:先看n最大才5,所以很容易想到暴力写法,因为最近在学后缀自动机就写写后缀自动机吧。

   我们将第一个串作为母串,然后在用其他的串与它进行匹配,并且记录下其匹配中每个状态的最大匹配数,答案则为每个状态的最大匹配的最小值中的最大值。。(绕晕了)

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define N 4005
using namespace std;
int last,root,tot;
char s[N];
int n,m,ans;
struct data{
int son[N][],fa[N],val[N],ans[N],sum[N],tmp[N],smin[N];
void prepare(){root=last=tot=;}
int newnode(int x){val[++tot]=x; return tot;}
void extend(int x)
{
int p=last,np=newnode(val[p]+);
for (; p && !son[p][x]; p=fa[p]) son[p][x]=np;
if (!p) fa[np]=root;
else
{
int q=son[p][x];
if (val[p]+==val[q]) fa[np]=q;
else
{
int nq=newnode(val[p]+);
memcpy(son[nq],son[q],sizeof(son[q]));
fa[nq]=fa[q]; fa[q]=fa[np]=nq;
for (; p&& son[p][x]==q; p=fa[p]) son[p][x]=nq;
}
}
last=np;
}
void sort()
{
memset(sum,,sizeof(sum));
for (int i=; i<=tot; i++) ans[i]=val[i];
for (int i=; i<=tot; i++) sum[val[i]]++;
for (int i=; i<=tot; i++) sum[i]+=sum[i-];
for (int i=; i<=tot; i++) tmp[sum[val[i]]--]=i;
}
void work()
{
scanf("%s",s+); m=strlen(s+);
memset(smin,,sizeof(smin));int len=;last=root;
for (int i=; i<=m; i++)
{
int x=s[i]-'a';
if (son[last][x]) last=son[last][x],len++;
else
{
for (; last&&!son[last][x];)last=fa[last];
if (!last) len=,last=root;
else
{
len=val[last]+; last=son[last][x];
}
}
smin[last]=max(smin[last],len);
}
for (int i=tot; i>=; i--)
{
int x=tmp[i];
ans[x]=min(ans[x],smin[x]);
if (fa[x] && smin[x]) smin[fa[x]]=val[fa[x]];
}
}
}SAM;
int main()
{
scanf("%d\n",&n); n--;
SAM.prepare();
scanf("%s",s+); int len=strlen(s+);
for (int i=; i<=len; i++) SAM.extend(s[i]-'a');
SAM.sort();
for (int i=; i<=n; i++) SAM.work();
for (int i=; i<=tot; i++) ans=max(ans,SAM.ans[i]);//,cout<<i<<" "<<SAM.ans[i]<<endl;
printf("%d\n",ans);
}

注意:我们在写的过程中,每到一个状态我们要不断跟新之前的状态答案,我们可以用dfs或者利用right数组的特性来更新,具体细节看看代码。

[Poi2000]公共串 && hustoj2797的更多相关文章

  1. BZOJ 2946: [Poi2000]公共串

    2946: [Poi2000]公共串 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 787  Solved: 342[Submit][Status][D ...

  2. BZOJ 2946: [Poi2000]公共串( 后缀自动机 )

    一个串建后缀自动机, 其他串在上面跑, 然后用当前串跑的去更新全部 ------------------------------------------------------------------ ...

  3. BZOJ_2946_[Poi2000]公共串_后缀数组+二分答案

    BZOJ_2946_[Poi2000]公共串_后缀数组+二分答案 Description          给出几个由小写字母构成的单词,求它们最长的公共子串的长度. 任务: l        读入单 ...

  4. 【BZOJ2946】[Poi2000]公共串 后缀数组+二分

    [BZOJ2946][Poi2000]公共串 Description        给出几个由小写字母构成的单词,求它们最长的公共子串的长度. 任务: l        读入单词 l        计 ...

  5. 【BZOJ 2946】 2946: [Poi2000]公共串 (SAM)

    2946: [Poi2000]公共串 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 1063  Solved: 469 Description      ...

  6. 【bzoj2946】[Poi2000]公共串 后缀自动机

    [Poi2000]公共串 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 1386  Solved: 620[Submit][Status][Discus ...

  7. [POI2000] 公共串 - 后缀数组,二分

    [POI2000] 公共串 Description 给出几个由小写字母构成的单词,求它们最长的公共子串的长度. Solution 预处理出后缀数组和高度数组,二分答案 \(k\) ,对于每一个连续的 ...

  8. [bzoj2946][Poi2000]公共串_后缀数组_二分

    公共串 bzoj-2946 Poi-2000 题目大意:给定$n$个字符串,求他们的最长公共子串. 注释:$1\le n\le 5$,$1\le minlen<maxlen\le 2000$. ...

  9. [BZOJ2946] [Poi2000]公共串解题报告|后缀数组

    给出几个由小写字母构成的单词,求它们最长的公共子串的长度. 单词个数<=5,每个单词长度<=2000     尽管最近在学的是SAM...但是看到这个题还是忍不住想写SA... (其实是不 ...

随机推荐

  1. Eclipse最有用的快捷键

    编辑 Ctrl+1 快速修复(最经典的快捷键,就不用多说了,可以解决很多问题,比如import类.try catch包围等) Ctrl+Shift+F 格式化当前代码 Ctrl+Shift+M 添加类 ...

  2. git bash退回上一个文件夹

    cd ..\ a@w3311 MINGW32 /f/Projects/crm (master) $ cd..\ > bash: cd..: command not found a@w3311 M ...

  3. hdu_4467_Graph(莫队算法思想)

    题目连接:hdu_4467_Graph 题意:给你n个点,m条边,每条边有一个权值,有两个操作,一个是修改单点的颜色,一个是询问边的两个端点都为指定颜色的权值和 题解:这题如果暴力的话,就是维护3个a ...

  4. BAT54C 二极管是如何工作的?

    这是一个多电源供电的电路:Vcc是正常供电电源(如5V,由市电变换得到),电压大于(Vcc1-Vf),正常供电时二极管不导通:Vcc1是电池供电电源,当Vcc撤掉时,DD1(上边的二极管)导通,由Vc ...

  5. I2C死锁原因及解决方法(转)

    源:http://blog.csdn.net/zyboy2000/article/details/5603091 死锁总线表现为:SCL为高,SDA一直为低 现象:单片机采用硬件i2c读取E2PROM ...

  6. How to spend you day ?

    如果这是你生命中的最后的一天,你该如何去过好它呢? 不要浪费你生命中的每一分,每一秒!!!

  7. js变量数组

    <html><head lang="en"> <meta charset="UTF-8"> <title>< ...

  8. Mysql用户相关操作

    MySQL 默认有个root用户,但是这个用户权限太大,一般只在管理数据库时候才用.如果在项目中要连接 MySQL 数据库,则建议新建一个权限较小的用户来连接. 在 MySQL 命令行模式下输入如下命 ...

  9. IMapControl3 Interface(1) Properties属性

    IMapControl3 Interface Provides access to members that control the MapControl. Note: the IMapControl ...

  10. PHP中的ORM

    周末找个时间好好写一写 ORM相关的东西,整理整理. 参考:http://www.cnblogs.com/52fhy/p/5353181.html http://www.cnblogs.com/52f ...