poj 3450 Corporate Identity(后缀数组 多个字符串的最长公共字串)
来源:互联网 发布:网络信息有限公司 编辑:程序博客网 时间:2024/06/07 01:08
Description
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.
Input
The input contains several tasks. Each task begins with a line containing a positive integer N, the number of trademarks (2 ≤ N ≤ 4000). The number is followed by N lines, each containing one trademark. Trademarks will be composed only from lowercase letters, the length of each trademark will be at least 1 and at most 200 characters.
After the last trademark, the next task begins. The last task is followed by a line containing zero.
Output
For each task, output a single line containing the longest string contained as a substring in all trademarks. If there are several strings of the same length, print the one that is lexicographically smallest. If there is no such non-empty string, output the words “IDENTITY LOST” instead.
Sample Input
3aabbaabbabbababbbbbbbabb2xyzabc0
Sample Output
abbIDENTITY LOST
解:类似于二个字符串的操作,用id数组记录每一个下标属于哪个字符串,当二分枚举答案时如当前字符串恰好出现在所有字符串中出现过则返回真
清空数组开成bool 快了将近1s
#include <iostream>#include <stdio.h>#include <string.h>#include <stack>#include <queue>#include <map>#include <set>#include <vector>#include <math.h>#include <bitset>#include <algorithm>#include <climits>using namespace std;const int N = 1000000;int sa[N];int t1[N],t2[N],c[N];int rank1[N],height[N];void build_sa(int s[],int n ,int m){ int i,j,p,*x=t1,*y=t2; for(int i=0;i<m;i++) c[i]=0; for(int i=0;i<n;i++) c[x[i]=s[i]]++; for(int i=0;i<m;i++) c[i]+=c[i-1]; for(int i=n-1;i>=0;i--) sa[--c[x[i]]]=i; for(j=1;j<=n;j<<=1) { p=0; for(i=n-j;i<n;i++) y[p++]=i; for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j; for(i=0;i<m;i++) c[i]=0; for(i=0;i<n;i++) c[x[y[i]]]++; for(i=0;i<m;i++) c[i]+=c[i-1]; for(i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i]; swap(x,y); p=1;x[sa[0]]=0; for(i=1;i<n;i++) x[sa[i]]=(y[sa[i-1]]==y[sa[i]])&&(y[sa[i-1]+j]==y[sa[i]+j])?p-1:p++; if(p>=n) break; m=p; }}int s[N];void getheight(int s[],int n){ int i, j, k=0; for(i=1;i<=n;i++) rank1[sa[i]]=i;(1->n) for(i=0;i<n;i++) { if(k) k--; j=sa[rank1[i]-1]; while(s[i+k]==s[j+k]) k++; height[rank1[i]]=k; } return ;}char str1[N], str2[N];int id[N], sum;bool vis[4005];int judge(int mid,int n,int k){ memset(vis,0,sizeof(vis)); int i, j, cnt=0; for(int i=2;i<=n;i++) { if(height[i]<mid) { memset(vis,0,sizeof(vis)); cnt=0; } else { if(!vis[id[sa[i-1]]]) { cnt++; vis[id[sa[i-1]]]=1; } if(!vis[id[sa[i]]]) { cnt++; vis[id[sa[i]]]=1; } if(cnt==k) { sum=sa[i]; return 1; } } } return 0;}int main(){ int n, i, j, k, len; while(scanf("%d",&k),k!=0) { n=0; for(int i=0;i<k;i++) { scanf("%s",str1); len=strlen(str1); for(j=0;j<len;j++) { s[n]=str1[j];id[n]=i; n++; } s[n]='#'+i;id[n]='#'+i; n++; } s[n]=0; build_sa(s,n+1 ,5000); getheight(s,n); int l=1, r=len, mid, flag=0; while(l<=r) { mid=(l+r)/2; if(judge(mid,n,k)) flag=mid,l=mid+1; else r=mid-1; } if(flag) { for(j = 0; j<flag; j++) str2[j] = s[sum+j]; str2[flag] = '\0'; printf("%s\n",str2); } else printf("IDENTITY LOST\n"); } return 0;}
- poj 3450 Corporate Identity(后缀数组 多个字符串的最长公共字串)
- POJ 3450--->Corporate Identity(后缀数组求多个字符串的公共子串)
- Poj 3080 Blue Jeans + Hdu 2328 Corporate Identity (后缀数组 字典序最小的最长公共子串)
- POJ 3450 Corporate Identity(kmp求多个字符串的最长公共子串)
- 【后缀数组】 POJ 3450 Corporate Identity
- POJ 2774 Long Long Message(后缀数组 两个字符串的最长公共字串 )
- poj 3450 Corporate Identity (KMP+最长公共子串)
- POJ 3450 Corporate Identity(KMP:最长连续公共子序列)
- 多个字符串的最长公共字串
- 多个字符串的最长公共字串
- 后缀数组(多个字符串的最长公共子串)—— POJ 3294
- POJ 3450 Corporate Identity 求所有字符的最长公共子串
- poj 3450 Corporate Identity(后缀数组+二分)
- POJ 3450 Corporate Identity(后缀数组+二分)
- POJ-3450 Corporate Identity(KMP/后缀数组)
- POJ 1724 Corporate Identity 后缀数组
- HDU 2328 Corporate Identity(后缀数组-求多个串的最长共同子串)
- POJ3450:Corporate Identity(后缀数组)
- java 集合 总结
- spring ioc原理(看完后大家可以自己写一个spring)
- 2A-L1 Image as function
- Nginx+Tomcat负载均衡的配置
- Jupter Notebook生成目录操作
- poj 3450 Corporate Identity(后缀数组 多个字符串的最长公共字串)
- python3-元组、列表、字典
- 自己动手写操作系统(七)
- 百度前端秋招笔试编程题
- 我的第一个web小程序
- bzoj 2186(欧拉函数)
- 百度 核心系统工程师 2017校招笔试
- 浅谈虚拟机与物理机
- poj-Wireless Network