Gene and Gina have a particular kind of farm. Instead of growing animals and vegetables, as
it is usually the case in regular farms, they grow strings. A string is a sequence of characters.
Strings have the particularity that, as they grow, they add characters to the left and/or to the
right of themselves, but they never lose characters, nor insert new characters in the middle.
Gene and Gina have a collection of photos of some strings at different times during their growth.
The problem is that the collection is not annotated, so they forgot to which string each photo
belongs to. They want to put together a wall to illustrate strings growing procedures, but they
need your help to find an appropriate sequence of photos.
Each photo illustrates a string. The sequence of photos must be such that if si comes imme-
diately before si+1 in the sequence, then si+1 is a string that may have grown from si (i.e., si
appears as a consecutive substring of si+1). Also, they do not want to use repeated pictures,
so all strings in the sequence must be different.
Given a set of strings representing all available photos, your job is to calculate the size of the
largest sequence they can produce following the guidelines above.
Gene and Gina have a particular kind of farm. Instead of growing animals and vegetables, as it is usually the case in regular farms, they grow strings. A string is a sequence of characters. Strings have the particularity that, as they grow, they add characters to the left and/or to the right of themselves, but they never lose characters, nor insert new characters in the middle. 
 Gene and Gina have a collection of photos of some strings at different times during their growth. The problem is that the collection is not annotated, so they forgot to which string each photo belongs to. They want to put together a wall to illustrate strings growing procedures, but they need your help to find an appropriate sequence of photos.
Each photo illustrates a string. The sequence of photos must be such that if si comes immediately before si+1 in the sequence, then si+1 is a string that may have grown from si (i.e., si appears as a consecutive substring of si+1). Also, they do not want to use repeated pictures, so all strings in the sequence must be different.
Given a set of strings representing all available photos, your job is to calculate the size of the largest sequence they can produce following the guidelines above.
                --by spoj


大意是有些字符串,从中选一些首尾相接,要求是相邻两串中前串为后串的子串
求最多用多少串
统计以串x为最长串(总母串)的最大方案,再枚举x取max
统计的方法是:
在标记fail指针时,
若x上有is_end标记,则把fail(x)与fa(x)的方案取max再+1作为x的方案;
若x上无is_end标记,虽然理论上不该有方案,但为了递推方便还是把fail(x)与fa(x)的方案取max(不+1)作为方案,不影响结果
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s[];
struct Trie{
int ch[];
}data[];
int tot;
int is_end[],fail[];
int que[];
void work(int );
void Init();
void buildfail();
int main()
{
int n;
while(scanf("%d",&n)&&n)
work(n);
return ;
}
void work(int n){
int i,j,k,len,ans=;
Init();
for(i=;i<=n;i++){
scanf("%s",s);
len=strlen(s);k=;
for(j=;j<len;j++){
if(!data[k].ch[s[j]-'a'])
data[k].ch[s[j]-'a']=++tot;
k=data[k].ch[s[j]-'a'];
}
is_end[k]=;
}
buildfail();
for(i=;i<=tot;i++)
if(ans<is_end[i])
ans=is_end[i];
printf("%d\n",ans);
}
void Init(){
memset(fail,,sizeof(fail));
memset(is_end,,sizeof(is_end));
memset(data,,sizeof(data));
tot=;
}
void buildfail(){
int h=,t=,i,j,k;
while(h<t){
h++;
for(i=;i<;i++)
if(data[que[h]].ch[i]){
que[++t]=data[que[h]].ch[i];
j=fail[que[h]];
while()
if(data[j].ch[i]&&data[j].ch[i]!=que[t]){
fail[que[t]]=data[j].ch[i];
is_end[que[t]]+=is_end[que[h]]>is_end[fail[que[t]]]?is_end[que[h]]:is_end[fail[que[t]]];
break;
}
else{
if(!j)break;
j=fail[j];
}
if(!fail[que[t]])is_end[que[t]]+=is_end[que[h]];
}
}
}

又及,第一次用SPOJ,感觉还不错;

【SPOJ】MGLAR10 - Growing Strings的更多相关文章

  1. 【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)

    [SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. ...

  2. 【SPOJ】Substrings(后缀自动机)

    [SPOJ]Substrings(后缀自动机) 题面 Vjudge 题意:给定一个长度为\(len\)的串,求出长度为1~len的子串中,出现最多的出现了多少次 题解 出现次数很好处理,就是\(rig ...

  3. 【SPOJ】Longest Common Substring II (后缀自动机)

    [SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...

  4. 【SPOJ】Longest Common Substring(后缀自动机)

    [SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...

  5. 【SPOJ】Distinct Substrings(后缀自动机)

    [SPOJ]Distinct Substrings(后缀自动机) 题面 Vjudge 题意:求一个串的不同子串的数量 题解 对于这个串构建后缀自动机之后 我们知道每个串出现的次数就是\(right/e ...

  6. 【SPOJ】Distinct Substrings/New Distinct Substrings(后缀数组)

    [SPOJ]Distinct Substrings/New Distinct Substrings(后缀数组) 题面 Vjudge1 Vjudge2 题解 要求的是串的不同的子串个数 两道一模一样的题 ...

  7. 【SPOJ】Power Modulo Inverted(拓展BSGS)

    [SPOJ]Power Modulo Inverted(拓展BSGS) 题面 洛谷 求最小的\(y\) 满足 \[k\equiv x^y(mod\ z)\] 题解 拓展\(BSGS\)模板题 #inc ...

  8. 【SPOJ】QTREE7(Link-Cut Tree)

    [SPOJ]QTREE7(Link-Cut Tree) 题面 洛谷 Vjudge 题解 和QTREE6的本质是一样的:维护同色联通块 那么,QTREE6同理,对于两种颜色分别维护一棵\(LCT\) 每 ...

  9. 【SPOJ】QTREE6(Link-Cut-Tree)

    [SPOJ]QTREE6(Link-Cut-Tree) 题面 Vjudge 题解 很神奇的一道题目 我们发现点有黑白两种,又是动态加边/删边 不难想到\(LCT\) 最爆力的做法,显然是每次修改单点颜 ...

随机推荐

  1. iOS学习笔记(5)——显示简单的TableView

    1. 创建工程 创建一个新的Xcode工程命名为SimpleTableTest. 删除main.storyboard文件和info.plist中有关storyboard的相关属性. 按command+ ...

  2. CentOS 7下安装RabbitMQ

    下载erlang:http://www.erlang.org/downloads ,otp_src_20.3.tar.gz 下载RabbitMQ: http://www.rabbitmq.com ,r ...

  3. Python and Bluetooth

    环境 Windows7-64bit + Python2.7.15-64bit + Pybluez 安装pybluez时会报错,处理方法~ 1.安装VCForPython27.msi,这是为了提供vis ...

  4. SpringSecurity之记住我功能的实现

    Spring security记住我基本原理: 登录的时候,请求发送给过滤器UsernamePasswordAuthenticationFilter,当该过滤器认证成功后,会调用RememberMeS ...

  5. java线程状态 以及 sheep()、wait()、yield() 区别

    前言 最近看到很多人都在讨论多线程的问题,于是写出了这篇博客,希望可以帮到正在学习和使用这块的朋友们,首先我们先看看两个图(两个图都来自其他码农的分享)   这两个图是一样的逻辑,这里一起罗列出来,下 ...

  6. Google Java 编程风格指南 —— 见微知著

    目录 前言 源文件基础 源文件结构 格式 命名约定 编程实践 Javadoc 后记 前言 这份文档是Google Java编程风格规范的完整定义.当且仅当一个Java源文件符合此文档中的规则, 我们才 ...

  7. window.location和document.location的区别分析

    用户不能改变document.location(因为这是当前显示文档的位置).但是,可以改变window.location (用其它文档取代当前文档)window.location本身也是一个对象,而 ...

  8. mysql中sql中的注释

    学习mysql好久了,oracle也是,但是经常使用oracle,有一天突然想,oracle的sql语法中有注释,那么mysql中是不是也有注释,于是从网上搜了一下,原来mysql中的注释还真不少,下 ...

  9. 打乱数组——shuffle

    在学习vue移动端音乐项目时,看到一个打乱数组函数,感觉很有意思就记录一下(意外发现:slice是个有趣的知识点) 原理:遍历数组,(let i = 0; i < _arr.length; i+ ...

  10. Command操作数据

    Command常用属性 CommandText             要下达至数据源的命令 CommandTimeout          出错时等待时间 //定义一个变量来存储对数据库操作的字符串 ...