Ural1517 Freedom of choice, 后缀数组,最长公共子串
来源:互联网 发布:中信淘宝信用卡好吗 编辑:程序博客网 时间:2024/05/17 00:05
也是看后缀数组中的套题。。。
/******************************************************************************* # Author : Neo Fung # Email : neosfung@gmail.com # Last modified: 2012-06-05 21:27 # Filename: URAL1517 Freedom of Choice.cpp # Description : 因为我的倍增算法要求除了字符串的最后一位以外,其它位不能为0,否则会出错 ******************************************************************************/#ifdef _MSC_VER// #define DEBUG// #define _CRT_SECURE_NO_DEPRECATE#endif#include <fstream>#include <stdio.h>#include <iostream>#include <string.h>#include <string>#include <limits.h>#include <algorithm>#include <math.h>#include <numeric>#include <functional>#include <ctype.h>using namespace std;const int kMAXN=210010;int w[kMAXN],wa[kMAXN],wb[kMAXN],wv[kMAXN],array[kMAXN],sa[kMAXN];// int cmp(const int *r,const int &a,const int &b,const int &l){ return r[a]==r[b]&&r[a+l]==r[b+l];}// 倍增算法// r为待比较数组;n为r的长度;m为r中元素的最大值 void DoublingAlgorithm(const int *r,int *sa,const int &n,int m){ int i,j,p,*x=wa,*y=wb,*t; for (i=0;i<m;i++) w[i]=0; for (i=0;i<n;i++) w[x[i]=r[i]]++; for (i=1;i<m;i++) w[i]+=w[i-1]; for (i=n-1;i>=0;i--) sa[--w[x[i]]]=i; for (p=1,j=1;p<n;j*=2,m=p){ for (p=0,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++) w[i]=0; for (i=0;i<n;i++) w[wv[i]=x[y[i]]]++; for (i=1;i<m;i++) w[i]+=w[i-1]; for (i=n-1;i>=0;i--) sa[--w[wv[i]]]=y[i]; for (t=x,x=y,y=t,p=1,i=1,x[sa[0]]=0;i<n;i++) x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++; } return;}// 计算height数组,r和sa的定义同上;// height的下标从1开始,到n结束,包含n;int g_rank[kMAXN],height[kMAXN];void CalculateHeight(const int *r,int *sa,const int &n){ int i,j,k=0; for (i=1;i<=n;i++) g_rank[sa[i]]=i; for (i=0;i<n;height[g_rank[i++]]=k) for (k?k--:0,j=sa[g_rank[i]-1];r[i+k]==r[j+k];k++); return;}int main(void){#ifdef DEBUG freopen("../stdin.txt","r",stdin);freopen("../stdout.txt","w",stdout); #endif int n,ncase=1;char str[kMAXN];// scanf("%d",&ncase);while(~scanf("%d",&n) && n){getchar();gets(str);for(int i=0;i<n;++i)array[i]=str[i];array[n]=1;gets(str);for(int i=0;i<n;++i)array[i+n+1]=str[i];array[n+n+2]=0;DoublingAlgorithm(array,sa,n+n+2,256);CalculateHeight(array,sa,n+n+1);int max_height=1,idx=0;for(int i=1;i<n+n+2;++i){if(max_height<height[i]){int a=sa[i-1],b=sa[i];if(a>b)swap(a,b);if(a<n && b>n){max_height=height[i];idx=a;}}}for(int i=0;i<max_height;++i)printf("%c",array[i+idx]);printf("\n");// solve(n);}return 0;}
- Ural1517 Freedom of choice, 后缀数组,最长公共子串
- URAL 1517 Freedom of Choice (后缀数组 输出两个串最长公共子串)
- URAL 1517. Freedom of choice(后缀数组:最长公共连续子串)
- URAL 1517. Freedom of choice(后缀数组:最长公共连续子串)
- URAL1517-Freedom of Choice
- URAL 题目1517. Freedom of Choice(后缀数组,求公共最长串)
- timus 1517 Freedom of Choice//后缀数组
- Ural 1517. Freedom of Choice 后缀数组
- URAL 1517 Freedom of Choice 后缀数组
- ural1517之最长公共子串
- URAL 1517 Freedom of Choice 后缀数组 入门
- 最长公共子串--后缀数组实现
- HDU1403(后缀数组--最长公共子串)
- 最长公共子串(后缀数组)
- 最长公共子串(后缀数组)
- 【poj2774】 后缀数组最长公共子串
- 后缀数组 最长公共子串
- [CODEVS3160]最长公共子串|后缀数组|后缀自动机
- 集合框架
- SQL Server 2012 开发新特性:文件表
- Drupal 7如何实现在后台以表格,列表的形式管理网站的内容?像数据库表一样?
- 基于stun,turn ,ice协议的 SBC服务器
- Matlab优化函数linprog的使用
- Ural1517 Freedom of choice, 后缀数组,最长公共子串
- 利用Apache的commons-net实现FTP的文件读取、上传、下载、删除和移动功能
- SQL Server 2012 安全新特性:包含数据库
- Android 下拉列表 Spinner
- mysql入门篇2——常用数据字典信息
- 以太网类型码(Ethernet type codes)
- A盾 (A-Protect) 枚举进程的一个bug
- adobe flash player设置时无法点击允许的解决方法
- 2012.6辽宁省赛 Problem F