POJ 3450 Corporate Identity(kmp求多个字符串的最长公共子串)
来源:互联网 发布:java工程师年龄要求 编辑:程序博客网 时间:2024/06/05 02:57
http://poj.org/problem?id=3450
#include <stdio.h>#include <string.h>const int max_N=4001;const int max_l=201;int n;char res[max_l],dict[max_N][max_l];int nexts[max_N];int min(int a,int b){return a<b?a:b;}int max(int a,int b){return a>b?a:b;}void get_nexts(char *s,int len){ memset(nexts,0,sizeof(nexts)); for(int i=1,j=0;i<len;) { if(s[i]==s[j])nexts[i++]=++j; else if(j>0) j=nexts[j-1]; else i++; }}int get_longestpre(char *s,int len){ get_nexts(s,len); for(int i=1;i<n;i++) { char *p=dict[i]; int j=0,tmp=0; for(;*p&&j<len;) { if(*p==s[j]) { p++,j++; tmp=max(tmp,j); } else if(j>0) j=nexts[j-1]; else p++; } len=tmp; } return len;}int main(){ while(scanf("%d",&n)&&n) { getchar(); for(int i=0;i<n;i++) { gets(dict[i]); } int len=strlen(dict[0]),ans=0,pos=0; for(int i=0;i<len;i++) { int tmp=get_longestpre(dict[0]+i,len-i); if(tmp>=ans) { if(tmp>ans) { ans=tmp; pos=i; } else { bool smaller =true; for(int t=0;t<ans;t++) { if(dict[0][pos+t]>dict[0][i+t]) break; else if(dict[0][pos+t]<dict[0][i+t]) { smaller=0; break; } } if(smaller) pos=i; } } } if(ans) { for(int i=0;i<ans;i++) putchar(dict[0][pos+i]); printf("\n"); } else puts("IDENTITY LOST"); } return 0;}
详情请见:http://www.cnblogs.com/mfrbuaa/p/5027323.html 这位大神的微博
0 0
- POJ 3450 Corporate Identity(kmp求多个字符串的最长公共子串)
- poj 3450 Corporate Identity (KMP+最长公共子串)
- POJ 3450 Corporate Identity(KMP:最长连续公共子序列)
- POJ 3450--->Corporate Identity(后缀数组求多个字符串的公共子串)
- poj 3450 Corporate Identity 【KMP】【求公共子串】
- POJ3450 Corporate Identity(kmp,最长公共子串)
- POJ 3450 Corporate Identity 求所有字符的最长公共子串
- poj--3450 KMP求多个字符串的最长公共子串
- POJ3450 Corporate Identity 最长公共子串
- poj 3450 Corporate Identity(后缀数组 多个字符串的最长公共字串)
- Poj 3080 Blue Jeans + Hdu 2328 Corporate Identity (后缀数组 字典序最小的最长公共子串)
- 【poj 3450 Corporate Identity 】 KMP(暴力)
- POJ-3450-Corporate Identity(KMP)
- HDU 2328 Corporate Identity(Trie·最长公共子串)
- 【KMP求多个串的最长公共子串】POJ 3450
- POJ 3450 Corporate Identity (KMP+暴搞)
- poj 3450 Corporate Identity(二分长度 + 暴力kmp)
- POJ 题目3450 Corporate Identity(KMP 暴力)
- 杭电5562之Clarke and food
- 非空子集个数
- wireshark捕获/过滤指定ip地址数据包
- constantize()函数
- Graham求凸包
- POJ 3450 Corporate Identity(kmp求多个字符串的最长公共子串)
- [HDU 2222] Keywords Search [AC自动机]
- 剑指offer 37题 【时间空间效率的平衡】两个链表的第一个公共结点
- 二、词法结构
- ISO12233分辨率测试卡的使用方法
- tjut 4920
- ubuntu14.04搭建hadoop集群
- SVM入门学习总结
- dedecms织梦TAG标签显示每个标签共有多少篇文章的方法