POJ 2774 Long Long Message(SA 求最长公共子串)
来源:互联网 发布:python requests爬虫 编辑:程序博客网 时间:2024/06/09 00:37
题目链接:Click here~~
题意:
RT。
解题思路:
类似地,将第二个串接到第一个串的后面,中间用特殊字符隔开。得到height[]后,找出分别属于两个串的后缀的 lcp 的最大值就好了。
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;const int N = 2e5 + 5;int sa[N],rank[N],rank2[N],height[N],cnt[N],*x,*y;/* * a radix_sort which is based on the y[]. * how ? ahhhh, the last reverse for is the solution. * and the adjacant value of sa[] might have the same rank.*/void radix_sort(int n,int sz){ memset(cnt,0,sizeof(cnt)); for(int i=0;i<n;i++) cnt[ x[ y[i] ] ]++; for(int i=1;i<sz;i++) cnt[i] += cnt[i-1]; for(int i=n-1;i>=0;i--) sa[ --cnt[ x[ y[i] ] ] ] = y[i];}/* * sa[i] represents the ith suffix string is which one. * rank[i] represents the suffix string [i,n]'s rank. * sz is the max_rank of text in that time. * x[] represents the true pointer of rank[] in that time and it may be not unique. * y[] is the location of text[] which is sorted by 2nd key in that time before swap(x,y).*/void get_sa(char text[],int n,int sz=128){ x = rank, y = rank2; for(int i=0;i<n;i++) x[i] = text[i], y[i] = i; radix_sort(n,sz); for(int len=1;len<n;len<<=1) { int yid = 0; for(int i=n-len;i<n;i++) y[yid++] = i; for(int i=0;i<n;i++) if(sa[i] >= len) y[yid++] = sa[i] - len; radix_sort(n,sz); swap(x,y); x[ sa[0] ] = yid = 0; for(int i=1;i<n;i++) { if(y[ sa[i-1] ]==y[ sa[i] ] && sa[i-1]+len<n && sa[i]+len<n && y[ sa[i-1]+len ]==y[ sa[i]+len ]) x[ sa[i] ] = yid; else x[ sa[i] ] = ++yid; } sz = yid + 1; if(sz >= n) break; } for(int i=0;i<n;i++) rank[i] = x[i];}/* * height[] represents the longest common prefix of suffix [i-1,n] and [i,n]. * height[ rank[i] ] >= height[ rank[i-1] ] - 1. ..... let's call [k,n] is the suffix which rank[k] = rank[i-1] - 1, ...=> [k+1,n] is a suffix which rank[k+1] < rank[i] ..... and the lcp of [k+1,n] and [i,n] is height[ rank[i] ] - 1. ..... still unknow ? height[ rank[i] ] is the max lcp of rank[k] and rank[i] which rank[k] < rank[i].*/void get_height(char text[],int n){ int k = 0; for(int i=0;i<n;i++) { if(rank[i] == 0) continue; k = max(0,k-1); int j = sa[ rank[i]-1 ]; while(i+k<n && j+k<n && text[i+k]==text[j+k]) k++; height[ rank[i] ] = k; }}namespace RMQ{ int dp[20][N]; void init(int c[],int n) { for(int i=0;i<n;i++) dp[0][i] = c[i]; for(int j=1;j<20;j++) for(int i=0;i+(1<<j)-1<n;i++) dp[j][i] = min(dp[j-1][i],dp[j-1][i+(1<<(j-1))]); } int _log2(int n) { int ret = 0; while(1<<(ret+1) <= n) ret++; return ret; } int get_min(int a,int b) { int k = _log2(b-a+1); return min(dp[k][a],dp[k][b-(1<<k)+1]); }}int lcp(int a,int b){ a = rank[a] , b = rank[b]; if(a > b) swap(a,b); return RMQ::get_min(a+1,b);}char str[N],str2[N];int main(){ //freopen("in.ads","r",stdin); //freopen("out.ads","w",stdout); while(~scanf("%s%s",str,str2)) { int n1 = strlen(str); int n = n1; str[n++] = 127; for(int i=0;str2[i];i++) str[n++] = str2[i]; str[n] = '\0'; get_sa(str,n); get_height(str,n); int ans = 0; for(int i=1;i<n;i++) if(sa[i-1] < n1 && sa[i] > n1 || sa[i-1] > n1 && sa[i] < n1) ans = max(ans,height[i]); printf("%d\n",ans); } return 0;}
- POJ 2774 Long Long Message(SA 求最长公共子串)
- POJ 2774 Long Long Message(最长公共子串)
- poj 2774 Long Long Message(最长公共子串)
- POJ 2774 Long Long Message(最长公共子串)
- POJ 2774 Long Long Message 求两个串最长公共子串(后缀数组)
- POJ 2774 Long Long Message(后缀数组求最长公共子串,4级)
- POJ 题目2774 Long Long Message(后缀数组,求最长公共子串长度)
- POJ 2774 Long Long Message(2.3 求两个字符串的最长公共子串)
- POJ 2774 Long Long Message (后缀数组求最长公共子串)
- poj 2774 Long Long Message,后缀数组,求最长公共子串 hdu1403
- POJ 2774 long long message(后缀数组求最长公共子串)
- POJ 2774 Long Long Message 后缀数组求最长公共子串
- POJ 2774 Long Long Message(最长公共子串 -初学后缀数组)
- Long Long Message+POJ+后缀数组之求最长公共子串
- POJ2774---Long Long Message(后缀数组,求最长公共子串)
- 【POJ2774】Long Long Message-求最长公共子串(后缀数组求法)
- POJ 2774 Long Long Message+Hdu 1403 Longest Common Substring (后缀数组 最长公共子串)
- 【后缀数组】 POJ 2774 Long Long Message 两个字符串的最长公共子串长度
- Android之TelephonyManager类的方法详解
- 3.2栈的应用举例
- 13个要重点关注的数据指标
- HDU 1575 Tr A
- DFS 和排列组合
- POJ 2774 Long Long Message(SA 求最长公共子串)
- Linux下使用system()函数一定要谨慎
- poj2914无向图的最小割模板
- iOS多线程编程之Grand Central Dispatch(GCD)介绍和使用
- node-sqlite3源码分析 - Database::run方法
- Cisco3550三层交换机vlan间路由配置实例
- mysql 自动备份一系列问题
- Discuz!X 中积分操作函数以及自定义积分
- SQLSERVER 存储过程中动态SQL的使用