【bzoj1031】 JSOI2007—字符加密Cipher

http://www.lydsy.com/JudgeOnline/problem.php?id=1031 (题目链接)

题意

  给出一个字符串,求它的加密串。

Solution

  很显然,将串倍长后求它的后缀数组,然后扫一遍就可以了

细节

  数组开两倍

代码

// bzoj1031
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<ctime>
#define LL long long
#define inf 1<<30
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std;

const int maxn=200010;
char s[maxn];
int sa[maxn],wa[maxn],wb[maxn],ww[maxn];

bool cmp(int *r,int a,int b,int l) {
	return r[a]==r[b] && r[a+l]==r[b+l];
}
void da(char *r,int *sa,int n,int m) {
	int i,j,p,*x=wa,*y=wb;
	for (i=0;i<=m;i++) ww[i]=0;
	for (i=1;i<=n;i++) ww[x[i]=r[i]]++;
	for (i=1;i<=m;i++) ww[i]+=ww[i-1];
	for (i=n;i>=1;i--) sa[ww[x[i]]--]=i;
	for (p=0,j=1;p<n;j*=2,m=p) {
		for (p=0,i=n-j+1;i<=n;i++) y[++p]=i;
		for (i=1;i<=n;i++) if (sa[i]>j) y[++p]=sa[i]-j;
		for (i=0;i<=m;i++) ww[i]=0;
		for (i=1;i<=n;i++) ww[x[y[i]]]++;
		for (i=1;i<=m;i++) ww[i]+=ww[i-1];
		for (i=n;i>=1;i--) sa[ww[x[y[i]]]--]=y[i];
		for (swap(x,y),p=x[sa[1]]=1,i=2;i<=n;i++)
			x[sa[i]]=cmp(y,sa[i-1],sa[i],j) ? p : ++p;
	}
}

int main() {
	scanf("%s",s+1);
	int n=strlen(s+1);
	for (int i=1;i<=n;i++) s[n+i]=s[i];
	int l=n+n;
	da(s,sa,l,300);
	for (int i=1;i<=l;i++)
		if (sa[i]<=n) printf("%c",s[sa[i]+n-1]);
    return 0;
}

 

This passage is made by MashiroSky.
posted @ 2017-01-13 17:22  MashiroSky  阅读(109)  评论(0编辑  收藏  举报