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. 开源代码MyCommons

    MyCommons是我在开发Android App中,经过多个项目的实践和应用,上十次修改的,总结起来的代码,目的是希望大家能够快速的完成项目的开发. 主要也是参考了afinal和xutils2个框架 ...

  2. 总结day7 ---- 文件操作,读,写,追加,以及相关方法

    内容大纲 一:文件的基本操作, >常见问题 >encoding >绝对路径和相对路径的 二:文件的读写追加相关操作 >读(r, r+ ,rb,r+b) >写(w,w+,w ...

  3. JavaScript变量那些事

    引言 JavaScript的变量本质是松散类型的,也就是说其变量就是用于保存特定值的一个名字,变量的值和数据类型可以在脚本执行的生命周期中发生变化.这是一个很有趣很强大的特性,但是也是一个极容易出错误 ...

  4. Vue 父子组件传递方式

    问题: parent.vue <template> <div> 父组件 <child :childObject="asyncObject">&l ...

  5. Java Web 热部署

    热部署有多种方案,下面的方案是其中的一种. 暂时还没找到一种令人满意的方案. 1,配置WEB Server 去这里 (https://tomcat.apache.org/download-90.cgi ...

  6. C#-输入输出,类型,运算符,语句的练习——★判断年份是否是闰年★

    //输入一个年份,判断是否是闰年 //(能被4整除却不能被100整除的,年份世纪年份能被400整除的是闰年) Console.Write("请输入一个年份:"); int year ...

  7. 采用prometheus 监控mysql

    1. prometheus 是什么 开源的系统监控和报警工具,监控项目的流量.内存量.负载量等实时数据. 它通过直接或短时jobs中介收集监控数据,在本地存储所有收集到的数据,并且通过定义好的rule ...

  8. jQuery 获取元素当前位置offset()与position()

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...

  9. linux系统服务管理

    centos7的服务管理命令 systemctl start 服务名称 systemctl stop 服务名称 systemctl status 服务名称 systemctl restart 服务名称 ...

  10. vue-cli3预设preset记录

    这两天公司搭建新项目的时候发现vue-cli3有一个神奇的的东西:preset(预设).preset其实是你在create新vue项目的时候,生成的插件配置项预设,也就是你在项目中需要用到的插件安装成 ...