题目大意:给你N(2-4000)个字符串,求出来他们的共同子串
 
分析:因为上次就说了再出现这种题就不用那种暴力的做法了,于是看了一些别的知识,也就是后缀树,把一个字符串的所有的后缀全部都加入字典树,然后用别的串去匹配,这样匹配的时候速度那是飕飕的啊,不过第一次我把前N-1个串的所有前缀搞进了字典树里面,然后想如果某个节点被访问N-1次,并且第N个串也能访问到此节点,那么这一定就是他们的共同子串了,不过总归是太天真,直接返回MLE,一细琢磨,想着最糟糕的情况也就是有8000(N)个串,每个串都不相同,并且每个串的长度是200(len),那么空间复杂度应该是 len*(len+1)/2*N*26 大约理论上最糟糕的情况就是41亿内存,不超才见鬼了呢于是又换一种想法,如果只把第一个串的所有后缀加入字典树不就行了?因为求的是共同子串,第一个串也肯定包括所有串的共同子串,这样内存开销就会降到len*(len+1)/2*26大约50w不到,比较容易接受了。
 
下面是AC代码:
==========================================================================================================
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
using namespace std; const int MAXN = ;
const int MAXM = ;
const int oo = 1e9+; struct node
{
int times;
node *next[MAXN];
}; int BuildTrie(node *head, char s[], int x)
{
int i, k, depth = ;
node *P = head; for(i=; s[i]; i++)
{
k = s[i] - 'a';
if(P->next[k] == NULL)
{
if(x != )
break;
P->next[k] = new node();
} P = P->next[k]; if(P->times + >= x)
{///如果此节点是本个串访问过或者上个节点访问过
P->times = x;
depth++;
}
else break;
} return depth;
}
void clearTrie(node *head)
{///销毁树
node *P = head; for(int i=; i<MAXN; i++)
{
if(P->next[i] != NULL)
clearTrie(P->next[i]);
} free(P);
} int main()
{
int i, j, N; while(scanf("%d", &N), N)
{
node *head = new node();
char s[MAXM]={}, ans[MAXM]={}; for(i=; i<N; i++)
{
scanf("%s", s);
for(j=; s[j] != '\0'; j++)
BuildTrie(head, s+j, i);
}
scanf("%s", s); int Max = ; for(j=; s[j] != '\0'; j++)
{
int len = BuildTrie(head, s+j, N);
char p[MAXM] = {}; strncpy(p, s+j, len); if(Max < len || (Max==len && strcmp(ans, p) > ))
strcpy(ans, p), Max = len;
} if(ans[] == )
printf("IDENTITY LOST\n");
else
printf("%s\n", ans); clearTrie(head);
} return ;
}

Corporate Identity - HDU 2328(多串求共同子串)的更多相关文章

  1. POJ 3450 Corporate Identity (KMP,求公共子串,方法很妙)

    http://blog.sina.com.cn/s/blog_74e20d8901010pwp.html我采用的是方法三. 注意:当长度相同时,取字典序最小的. #include <iostre ...

  2. hdu 2328 Corporate Identity(kmp)

    Problem Description Beside other services, ACM helps companies to clearly state their “corporate ide ...

  3. (KMP 暴力)Corporate Identity -- hdu -- 2328

    http://acm.hdu.edu.cn/showproblem.php?pid=2328 Corporate Identity Time Limit: 9000/3000 MS (Java/Oth ...

  4. POJ-3450 Corporate Identity (KMP+后缀数组)

    Description Beside other services, ACM helps companies to clearly state their “corporate identity”, ...

  5. hdu2328 Corporate Identity

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=2328 题目: Corporate Identity Time Limit: 9000/3000 MS (J ...

  6. POJ3450 Corporate Identity 【后缀数组】

    Corporate Identity Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 7662   Accepted: 264 ...

  7. POJ3450 Corporate Identity —— 后缀数组 最长公共子序列

    题目链接:https://vjudge.net/problem/POJ-3450 Corporate Identity Time Limit: 3000MS   Memory Limit: 65536 ...

  8. hdu2328 Corporate Identity【string库使用】【暴力】【KMP】

    Corporate Identity Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. N - Corporate Identity

    Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...

随机推荐

  1. OC - 23.核心动画基础

    概述 简介 核心动画提供了一组非常强大的动画API,通过该组API可以高效的实现绝大部分绚丽的动画效果 注意事项 核心动画的操作在子线程中执行,不会阻塞主线程 核心动画直接作用与CALayer对象上, ...

  2. Linux命令:tail命令详解

    概述:tail命令显示文件末尾区块,也可以查看线上日志 1.格式 tail [参数][文件] 2.参数 -f 循环读取 -q 不显示处理信息 -v 显示详细的处理信息 -c<数目> 显示的 ...

  3. 基于ProGuard-Maven-Plugin的自定义代码混淆插件

    介绍 大家可能都会碰到一些代码比较敏感的项目场景,这个时候代码被反编译看到就不好了,这个时候就需要代码混淆插件来对代码进行混淆了. 基于Maven的项目一般会去考虑使用proguard-maven-p ...

  4. Cocos Studio1.5.0.1开发学习笔记(一)

    听说Cocos Studio很久了,主要是因为骨骼动画.目前看来Cocos2d-x播放动画的方式只有2种: 第一种:是播放序列帧动画,即将动画的每一帧都加载进缓存里,需要播放时再使用Animation ...

  5. confluence5.8.10的使用

    之前在windows上安装了confluence5.8.10,结果有一天知什么缘故,数据库数据损坏,知识库彻底打不开了,所有的文档都付之东流,真的不是一般心痛.因此考虑将其装到linux机器上,因为t ...

  6. shell用if

    --------- shell用if出错了,Why? shell if 实例: site=github.com/fankcoder if [ $site == github.com/fankcoder ...

  7. ng-class css样式

    <style> .error{background-color: red;} .warning{background-color: yellow;} </style> < ...

  8. Apache:To Config The Vhost of Django Project

    It is not a good idea to use dev server in Production Environment. Apache or Nginx are good choice.B ...

  9. 快速访问WCF服务--ServiceModel 元数据实用工具 (Svcutil.exe)

    基本定义 ServiceModel 元数据实用工具用于依据元数据文档生成服务模型代码,以及依据服务模型代码生成元数据文档. SvcUtil.exe ServiceModel 元数据实用工具可在 Win ...

  10. 关于SQL server的一些知识点

    关于怎么打开xp_cmdshell的方法: exec sp_configure 'show advanced option',1reconfiguregoexec sp_configure 'xp_c ...