2015-2016 下半学期 第八周 训练(2)

1、poj3261

题意:求出现k次的最长可重复子串长度。

思路:

先二分答案,然后将后缀分成若干组。判断的是有没有一个组的后缀个数不小于k。如果有,那么存在k 个相同的子串满足条件,否则不存在。这个做法的时间复杂度为O(nlogn)。

也可以采取双指针法,滑一个长度为k-1的窗,使得min<=mid。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int nMax =1000012;

int  num[nMax];
int sa[nMax], rank[nMax], height[nMax];
int wa[nMax], wb[nMax], wv[nMax], wd[nMax];

int cmp(int *r, int a, int b, int l){
    return r[a] == r[b] && r[a+l] == r[b+l];
}

void da(int *r, int n, int m){          //  倍增算法 r为待匹配数组  n为总长度 m为字符范围
    int i, j, p, *x = wa, *y = wb, *t;
    for(i = 0; i < m; i ++) wd[i] = 0;
    for(i = 0; i < n; i ++) wd[x[i]=r[i]] ++;
    for(i = 1; i < m; i ++) wd[i] += wd[i-1];
    for(i = n-1; i >= 0; i --) sa[-- wd[x[i]]] = i;
    for(j = 1, p = 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 < n; i ++) wv[i] = x[y[i]];
        for(i = 0; i < m; i ++) wd[i] = 0;
        for(i = 0; i < n; i ++) wd[wv[i]] ++;
        for(i = 1; i < m; i ++) wd[i] += wd[i-1];
        for(i = n-1; i >= 0; i --) sa[-- wd[wv[i]]] = y[i];
        for(t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; i ++){
            x[sa[i]] = cmp(y, sa[i-1], sa[i], j) ? p - 1: p ++;
        }
    }
}

void calHeight(int *r, int n){           //  求height数组。
    int i, j, k = 0;
    for(i = 1; i <= n; i ++) rank[sa[i]] = i;
    for(i = 0; i < n; height[rank[i ++]] = k){
        for(k ? k -- : 0, j = sa[rank[i]-1]; r[i+k] == r[j+k]; k ++);
    }
}

int loc[nMax],m;
char str[nMax],res[nMax];
bool vis[1004];
int abs(int a){if(a>0)return a;return -a;}

bool check(int mid,int len,int k){   //长度为mid的子串是否出现了k次
    int i,j,tot=0;
    for(i=2;i<=len;i++){
        if(height[i]<mid)tot=0;
        else{
            tot++;
            if(tot==k-1)return 1;
        }
    }
    return 0;
}

int main(){
    int i,j,n,ans,k;
    while(scanf("%d%d",&n,&k)!=EOF){
        ans=0;
        for(i=0;i<n;i++){
            scanf("%d",&num[i]);
            num[i]++;
        }
        num[n]=0;
        da(num,n+1,1000002);
        calHeight(num,n);
        int left=0,right=n,mid;//开始二分
        while(right>=left){
            mid=(right+left)/2;
            if(check(mid,n,k)){
                left=mid+1;
                ans=mid;
            }
            else{
                right=mid-1;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

2、 

题意:裸题

思路:裸

代码:

#include <algorithm>
#include <cmath>
#include <queue>
#include <iostream>
#include <cstring>
#include <map>
#include <cstdio>
#include <vector>
#include <functional>
#define lson (i<<1)
#define rson ((i<<1)|1)
using namespace std;
typedef long long ll;
const int maxn = 1005;

int t1[maxn],t2[maxn],c[maxn];
bool cmp(int *r,int a,int b,int l)
{
    return r[a]==r[b] &&r[l+a] == r[l+b];
}

void get_sa(int str[],int sa[],int Rank[],int height[],int n,int m)
{
    n++;
    int 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] = str[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; j <= n; j <<= 1)
    {
        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++)
            x[sa[i]] = cmp(y,sa[i-1],sa[i],j)? p-1:p++;
        if(p >= n) break;
        m = p;
    }
    int k = 0;
    n--;
    for(int i = 0; i <= n; i++)
        Rank[sa[i]] = i;
    for(int i = 0; i < n; i++)
    {
        if(k) k--;
        int j = sa[Rank[i]-1];
        while(str[i+k] == str[j+k]) k++;
        height[Rank[i]] = k;
    }
}

int Rank[maxn],height[maxn];
int sa[maxn];
char str[maxn];
int a[maxn];
int n;

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int tot = 0;
        scanf("%s",str);
        int len = strlen(str);
        for(int i = 0;i < len; i++)
            a[i] = str[i];
        a[len] = 0;
        get_sa(a,sa,Rank,height,len,200);

        int ans = len*(len+1)/2;
        for(int i = 2; i <= len; i++)
        {
            //printf("%d ",height[i]);
            ans -= height[i];
        }
        cout << ans <<endl;
    }
    return 0;
}

3、

题意:裸的

思路:裸

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define N 51000
char a[N];
int c[N],d[N],e[N],sa[N],height[N],n,b[N],m;
int cmp(int *r,int a,int b,int l)
{
    return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da()
{
    int i,j,p,*x=c,*y=d,*t;
    for(i=0;i<m;i++)b[i]=0;
    for(i=0; i<n; i++)b[x[i]=a[i]]++;
    for(i=1; i<m; i++)b[i]+=b[i-1];
    for(i=n-1; i>=0; i--)sa[--b[x[i]]]=i;
    for(j=1,p=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<n; i++)e[i]=x[y[i]];
        for(i=0;i<m;i++)b[i]=0;
        for(i=0; i<n; i++)b[e[i]]++;
        for(i=1; i<m; i++)b[i]+=b[i-1];
        for(i=n-1; i>=0; i--)sa[--b[e[i]]]=y[i];
        for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1; i<n; i++)
            x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
    }
}
void callheight()
{
    int i,j,k=0;
    for(i=1; i<n; i++)b[sa[i]]=i;
    n--;
    for(i=0; i<n; height[b[i++]]=k)
        for(k?k--:0,j=sa[b[i]-1]; a[i+k]==a[j+k]; k++);
}

int main()
{
    int t,i;
    cin>>t;
    for(i=0;i<t;i++)
    {
        scanf("%s",a);
        n=strlen(a);
        n++;
        m=200;
        da();
        callheight();
        int sum=0;
        for(int j=1;j<=n;j++)sum+=n-sa[j]-height[j];
        cout<<sum<<endl;
    }
}


你可能感兴趣的:(2015-2016 下半学期 第八周 训练(2))