首页 > 其他分享 >SPOJ LCS2 Longest Common Substring II

SPOJ LCS2 Longest Common Substring II

时间:2022-11-09 19:03:51浏览次数:45  
标签:Substring cnt LCS2 int ++ len next II fa


Description


A string is finite sequence of characters over a non-empty finite set Σ.

In this problem, Σ is the set of lowercase letters.

Substring, also called factor, is a consecutive sequence of characters occurrences at least once in a string.

Now your task is a bit harder, for some given strings, find the length of the longest common substring of them.

Here common substring means a substring of two or more strings.

Input

The input contains at most 10 lines, each line consists of no more than 100000 lowercase letters, representing a string.

Output

The length of the longest common substring. If such string doesn't exist, print "0" instead.

Example



Input: alsdfkjfjkdsal fdjskalajfkdsla aaaajfaaaa Output: 2



Notice: new testcases added


后缀自动机


#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 300005;
char s[maxn];

class SAM
{
const static int maxn = 500005; //节点个数
const static int size = 26; //字符的范围
const static char base = 'a'; //字符的基准

class node
{
public:
node *fa, *next[size];
int len, cnt[10];
node* clear(int x)
{
fa = 0; len = x;
memset(cnt, 0, sizeof(cnt));
memset(next, 0, sizeof(next));
return this;
}
}nd[maxn]; //节点的设置

node *root, *last; //根节点,上一个节点
int tot; //总节点数
public:
void clear()
{
last = root = &nd[tot = 0];
nd[0].clear(0);
} //初始化
void insert(char ch)
{
node *p = last, *np = nd[++tot].clear(p->len + 1);
last = np;
int x = ch - base;
while (p&&p->next[x] == 0) p->next[x] = np, p = p->fa;
if (p == 0) { np->fa = root; return; }

node* q = p->next[x];
if (p->len + 1 == q->len) { np->fa = q; return; }

node *nq = nd[++tot].clear(p->len + 1);
for (int i = 0; i < size; i++)
if (q->next[i]) nq->next[i] = q->next[i];
nq->fa = q->fa;
q->fa = np->fa = nq;
while (p &&p->next[x] == q) p->next[x] = nq, p = p->fa;
} //插入操作

void find(char *ch, int y)
{
node *s = root;
int maxlen = 0;
for (int i = 0; ch[i]; i++)
{
int x = ch[i] - base;
if (s->next[x])
{
s = s->next[x];
s->cnt[y] = max(s->cnt[y], ++maxlen);
}
else
{
while (s&&!s->next[x])
{
s->cnt[y] = max(s->cnt[y], maxlen);
s = s->fa;
}
if (s == 0) s = root,maxlen = 0;
else
{
maxlen = s->len + 1; s = s->next[x];
s->cnt[y] = max(s->cnt[y], maxlen);
}
}
}
}
void query(int x)
{
int ans = 0;
for (int i = tot; i; i--)
{
int u = nd[i].len;
for (int j = 0; j < x; j++) u = min(u, nd[i].cnt[j]);
ans = max(ans, u);
}
printf("%d\n", ans);
}
}sam;

int main()
{
scanf("%s", s);
sam.clear();
for (int i = 0; s[i]; i++) sam.insert(s[i]);
int j;
for (j = 0; ~scanf("%s", s); j++) sam.find(s, j);
sam.query(j);
return 0;
}



标签:Substring,cnt,LCS2,int,++,len,next,II,fa
From: https://blog.51cto.com/u_15870896/5838293

相关文章

  • HUST 1602 Substring
    DescriptionThisproblemisquieteasy. Initially,thereisastringA.   Thenwedothefollowingprocessinfinitytimes.  A:=A+......
  • SPOJ 705 New Distinct Substrings
    DescriptionGivenastring,weneedtofindthetotalnumberofitsdistinctsubstrings.InputT-numberoftestcases.T<=20;Eachtestcaseconsistsofonestr......
  • HDU 1403 Longest Common Substring
    ProblemDescriptionGiventwostrings,youhavetotellthelengthoftheLongestCommonSubstringofthem.Forexample:str1=bananastr2=ciana......
  • 基础(服务寄宿在IIS中)
    1、配置服务器IIS   安装好IIS相关服务,确保网站能够启动   建立网站2、可能出现的问题(安装了最新版的ASP.NET4.0)未能从程序集“System.ServiceModel,Version......
  • 代码随想录day48 | 198.打家劫舍 213.打家劫舍II 337.打家劫舍III
    198.打家劫舍题目|文章思路数组以及下标含义dp[j]:当偷到第j家时所能获得的最多的金钱递推公式dp[j]=max(dp[j-1],dp[j-2]+nums[i])当偷到第j家时,如果偷......
  • Yii2的ListView用法
    Yii2的ListView用法控制器publicfunctionactionListView(){$this->layout=false;$dataProvider=newActiveDataProvider(['......
  • 142. 环形链表 II
    142.环形链表II给定一个链表的头节点 head ,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。如果链表中有某个节点,可以通过连续跟踪next指针再次到达......
  • Yii2 DetailView用法
    Yii2DetailView用法控制器代码publicfunctionactionDetailView(){//获取一条记录$model=Resource::find()->with(['resourceType'])->where(['......
  • 遇到端口占用无法启动IIS Express服务器
    报错图片:上图所述由于端口被占用无法完成IISExpress的输出这时候要考虑到自己在Windows的IIS是不是配置了该端口?很明显,就是配置了一个8091的端口且还在启动中,这时候......
  • 代码随想录day44 | 完全背包 518. 零钱兑换 II 377. 组合总和 Ⅳ
    完全背包文章思路有N件物品和一个最多能背重量为W的背包。第i件物品的重量是weight[i],得到的价值是value[i]。每件物品都有无限个(也就是可以放入背包多次),求解将哪些物......