zoukankan      html  css  js  c++  java
  • poj2217 Secretary 后缀数组

    #include <iostream>
    #include <cstring>
    #include <string>
    #include <cstdio>
    using namespace std;
    int T, n, m, p, len, r[20005], c[20005], x[20005], y[20005], sa[20005];
    int rank[20005], ans, hei[20005];
    string s, t;
    void getSa(){
    	r[n++] = 0;
    	for(int i=0; i<m; i++)	c[i] = 0;
    	for(int i=0; i<n; i++)	c[x[i]=r[i]]++;
    	for(int i=1; i<m; i++)	c[i] += c[i-1];
    	for(int i=n-1; i>=0; i--)	sa[--c[x[i]]] = i;
    	for(int j=1; p<n; j*=2, m=p){
    		p = 0;
    		for(int i=n-j; i<n; i++)	y[p++] = i;
    		for(int i=0; i<n; i++)	if(sa[i]>=j)	y[p++] = sa[i] - j;
    		for(int i=0; i<m; i++)	c[i] = 0;
    		for(int i=0; i<n; i++)	c[x[y[i]]]++;
    		for(int i=1; i<m; i++)	c[i] += c[i-1];
    		for(int i=n-1; i>=0; i--)	sa[--c[x[y[i]]]] = y[i];
    		swap(x, y);
    		p = 1;
    		x[sa[0]] = 0;
    		for(int i=1; i<n; i++)
    			if(y[sa[i-1]]==y[sa[i]] && y[sa[i-1]+j]==y[sa[i]+j])
    				x[sa[i]] =  p - 1;
    			else
    				x[sa[i]] = p++;
    	}
    	n--;
    }
    void getLcp(){
    	int h=0;
    	for(int i=1; i<=n; i++)	rank[sa[i]] = i;
    	for(int i=0; i<n; i++){
    		if(h)	h--;
    		int j=sa[rank[i]-1];
    		while(r[i+h]==r[j+h])	h++;
    		hei[rank[i]] = h;
    	}
    }//构建height数组
    int main(){
    	cin>>T;
    	getline(cin, s);
    	while(T--){
    		getline(cin, s);
    		getline(cin, t);
    		len = s.size();
    		s = s + "$" + t;
    		n = s.size();
    		m = 128;
    		ans = p = 0;
    		for(int i=0; i<n; i++)	r[i] = (int)s[i];
    		getSa();
    		getLcp();
    		for(int i=2; i<=n; i++)
    			if((sa[i-1]<len)^(sa[i]<len))
    				ans = max(ans, hei[i]);
    		printf("Nejdelsi spolecny retezec ma delku %d.
    ", ans);
    	}
    	return 0;
    }
    
  • 相关阅读:
    Java
    maven打包
    maven
    memset用法祥解
    HTML5 canvas save和restore方法讲解
    修复无线链接时断时连
    VirtualBox内Linux系统怎样与Windows共享文件夹
    Ubuntu默认密码,及其修改
    mysql中char与varchar的区别分析
    javax.servlet包介绍
  • 原文地址:https://www.cnblogs.com/poorpool/p/8256790.html
Copyright © 2011-2022 走看看