Corporate Identity - HDU 2328(多串求共同子串)
#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(多串求共同子串)的更多相关文章
- POJ 3450 Corporate Identity (KMP,求公共子串,方法很妙)
http://blog.sina.com.cn/s/blog_74e20d8901010pwp.html我采用的是方法三. 注意:当长度相同时,取字典序最小的. #include <iostre ...
- hdu 2328 Corporate Identity(kmp)
Problem Description Beside other services, ACM helps companies to clearly state their “corporate ide ...
- (KMP 暴力)Corporate Identity -- hdu -- 2328
http://acm.hdu.edu.cn/showproblem.php?pid=2328 Corporate Identity Time Limit: 9000/3000 MS (Java/Oth ...
- POJ-3450 Corporate Identity (KMP+后缀数组)
Description Beside other services, ACM helps companies to clearly state their “corporate identity”, ...
- hdu2328 Corporate Identity
地址:http://acm.hdu.edu.cn/showproblem.php?pid=2328 题目: Corporate Identity Time Limit: 9000/3000 MS (J ...
- POJ3450 Corporate Identity 【后缀数组】
Corporate Identity Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 7662 Accepted: 264 ...
- POJ3450 Corporate Identity —— 后缀数组 最长公共子序列
题目链接:https://vjudge.net/problem/POJ-3450 Corporate Identity Time Limit: 3000MS Memory Limit: 65536 ...
- hdu2328 Corporate Identity【string库使用】【暴力】【KMP】
Corporate Identity Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- N - Corporate Identity
Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...
随机推荐
- 使用EMMET中的小坑
使用EMMET写HTML的时候,是一个非常爽的事情.但是今天我使用时,发现一个小坑.以前倒也没有注意,不过需要非常的小心. form[action="/process" metho ...
- 【转】iOS开发6:UIActionSheet与UIAlertView
原文: http://my.oschina.net/plumsoft/blog/42763 iOS程序中的Action Sheet就像Windows中的 “确定-取消”对话框一样,用于强制用户进行选择 ...
- JavaScript Boolean(布尔) 对象
创建 Boolean 对象 Boolean 对象代表两个值:"true" 或者 "false" 下面的代码定义了一个名为 myBoolean 的布尔对象: va ...
- java 迭代器iterator
对于如ArrayList<E>类的数据,常用iterator遍历. ArrayList<String> list = new ArrayList<String>() ...
- 浅析angular框架的cookie
相信接触过网页编程的基本上都知道cookie这个东西吧,一个毫不起眼,但是又十分的重要的东西,今天我们就来分析一下这个小东西,我们都知道客服端通过发送http请求到服务器请求我们的数据,当我们的服务器 ...
- JS & JQuery 动态添加 select option
因为是转载文章 在此标明出处,以前有文章是转的没标明的请谅解,因为有些已经无法找到出处,或者与其它原因. 如有冒犯请联系本人,或删除,或标明出处. 因为好的文章,以前只想收藏,但连接有时候会失效,所以 ...
- jstl--c:choose标签
今天使用c:choose标签,一直报错: 严重: Servlet.service() for servlet CheckIncome threw exceptionorg.apache.jasper. ...
- C++跨平台事件机制实现
今天看到有人在讨论C++标准没有提供类似操作系统层次的事件通知机制,如windows的事件内核对象.其实我想说的事,C++11标准里的互斥量及条件变量已经够帮我们实现类似的功能了. 刚编写了一个事件通 ...
- ubuntu 升级命令
apt-get update && apt-get dist-upgrade
- Js regular exprission
正则表达式用于字符串处理.表单验证等场合,实用高效.现将一些常用的表达式收集于此,以备不时之需. 匹配中文字符的正则表达式: [\u4e00-\u9fa5] 评注:匹配中文还真是个头疼的事,有了这个表 ...