hdu2328 Corporate Identity 扩展KMP
Beside other services, ACM helps companies to clearly state their “corporate identity”, which includes company logo but also other signs, like trademarks. One of such companies is Internet Building Masters (IBM), which has recently asked ACM for a help with their new identity. IBM do not want to change their existing logos and trademarks completely, because their customers are used to the old ones. Therefore, ACM will only change existing trademarks instead of creating new ones.
After several other proposals, it was decided to take all existing trademarks and find the longest common sequence of letters that is contained in all of them. This sequence will be graphically emphasized to form a new logo. Then, the old trademarks may still be used while showing the new identity.
Your task is to find such a sequence.
扩展KMP裸题
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std; const int maxn=;
int nxt[][maxn],ext[][maxn];
char s[][maxn];
int len[],ans[maxn]; void EKMP(char s[],char t[],int lens,int lent,int c){
int i,j,p,l,k;
nxt[c][]=lent;j=;
while(j+<lent&&t[j]==t[j+])j++;
nxt[c][]=j;
k=;
for(i=;i<lent;i++){
p=nxt[c][k]+k-;
l=nxt[c][i-k];
if(i+l<p+)nxt[c][i]=l;
else{
j=max(,p-i+);
while(i+j<lent&&t[i+j]==t[j])j++;
nxt[c][i]=j;
k=i;
}
} j=;
while(j<lens&&j<lent&&s[j]==t[j])j++;
ext[c][]=j;k=;
for(i=;i<lens;i++){
p=ext[c][k]+k-;
l=nxt[c][i-k];
if(l+i<p+)ext[c][i]=l;
else{
j=max(,p-i+);
while(i+j<lens&&j<lent&&s[i+j]==t[j])j++;
ext[c][i]=j;
k=i;
}
}
} int main(){
int n;
while(scanf("%d",&n)!=EOF&&n){
memset(ans,0x3f,sizeof(ans));
for(int i=;i<=n;++i)scanf("%s",s[i]);
for(int i=;i<=n;++i)len[i]=strlen(s[i]);
int maxx=;
for(int i=;i<len[];++i){
for(int j=;j<=n;++j){
int cnt=;
EKMP(s[j],s[]+i,len[j],len[]-i,j);
for(int k=;k<len[j];++k){
if(ext[j][k]>cnt)cnt=ext[j][k];
}
if(cnt<ans[i])ans[i]=cnt;
}
if(ans[i]>maxx)maxx=ans[i];
}
if(!maxx)printf("IDENTITY LOST\n");
else{
string str[];
int cnt=;
for(int i=;i<len[];++i)if(ans[i]==maxx){
str[++cnt]=string(s[]+i,ans[i]);
}
sort(str+,str+cnt+);
cout<<str[]<<endl;
}
}
return ;
}
hdu2328 Corporate Identity 扩展KMP的更多相关文章
- kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity
Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...
- hdu2328 Corporate Identity【string库使用】【暴力】【KMP】
Corporate Identity Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu2328 Corporate Identity
地址:http://acm.hdu.edu.cn/showproblem.php?pid=2328 题目: Corporate Identity Time Limit: 9000/3000 MS (J ...
- POJ 题目3450 Corporate Identity(KMP 暴力)
Corporate Identity Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 5493 Accepted: 201 ...
- POJ 3450 Corporate Identity (KMP,求公共子串,方法很妙)
http://blog.sina.com.cn/s/blog_74e20d8901010pwp.html我采用的是方法三. 注意:当长度相同时,取字典序最小的. #include <iostre ...
- POJ 3450 Corporate Identity (KMP+暴搞)
题意: 给定N个字符串,寻找最长的公共字串,如果长度相同,则输出字典序最小的那个. 找其中一个字符串,枚举它的所有的字串,然后,逐个kmp比较.......相当暴力,可二分优化. #include & ...
- POJ 3450 Corporate Identity(KMP)
[题目链接] http://poj.org/problem?id=3450 [题目大意] 求k个字符串的最长公共子串,如果有多个答案,则输出字典序最小的. [题解] 我们对第一个串的每一个后缀和其余所 ...
- HDU - 2328 Corporate Identity(kmp+暴力)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2328 题意:多组输入,n==0结束.给出n个字符串,求最长公共子串,长度相等则求字典序最小. 题解:(居 ...
- [HDU2328]Corporate Identity(后缀数组)
传送门 求 n 个串的字典序最小的最长公共子串. 和 2 个串的处理方法差不多. 把 n 个串拼接在一起,中间连上一个没有出现过的字符防止匹配过界. 求出 height 数组后二分公共子串长度给后缀数 ...
随机推荐
- weblogic相关
来源:http://zhidao.baidu.com/link?url=gqPaYovGiEHYPPPyI6PgPV6THVLr4nlj3CGCjY3mwm0-ERUnfuQz614ywAwYLnP6 ...
- vs2015 产品密钥
一.破解秘钥 企业版 HM6NR-QXX7C-DFW2Y-8B82K-WTYJV 专业版 HMGNV-WCYXV-X7G9W-YCX63-B98R2 二.破解步骤 1.安装vs2015 2 ...
- 字符与字符串3——char 的大小
字符变量占用内存的大小,也就是char类型声明的变量,这个变量占多少字节. 一字节 char c = 'A'; printf("%d,%d\n", sizeof(c),sizeof ...
- 单元测试UI
cnpm install -g --save mocha cnpm install -g --save chai cnpm install -g --save istanbul const {sho ...
- CentOS7安装配置Amanda
参考: https://wenku.baidu.com/view/881e0c998e9951e79a892759.html yum -y install amanda* http://blog ...
- CPU的硬件结构和汇编语言
(已更正) 这个问题包括CPU的硬件结构和汇编语言的范畴. 这里梳理一下. 首先, 题主"李建国"自问自答的部分说的是正确的, CPU的指令集是软件与CPU这两个层级之间的接口, ...
- libusb 终于搞好了
- MFC 关于new出一个新对话框时,退出对话框内存泄漏的问题解决
问题: 在进行点击按钮弹出对话框时,我是用了new来生成一个新的对话框,但是在新对话框关闭的时候,经过检查发现,新对话框存在内存泄漏问题. 原因: 因为使用了new,但是当时没有找到地方进行delet ...
- 判断input checkbox选中
$("#chexk").get(0).checked $("#chexk").is(':checked')
- xshell 评估过期
手头拮据的朋友可以通过下面方法绕过: https://www.netsarang.com/download/down_form.html?code=522 删除XShell. 到英文官网下载页找到XS ...