zoj1729后缀树组/最小表示

 

题目来源:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1729

题目分类:后缀树组/最小表法

此题心得:学习两个算法

时间:2011-7-21

Hidden Password


Time Limit: 1 Second      Memory Limit: 32768 KB


Some time the programmers have very strange ways to hide their passwords. See for example how Billy "Hacker" Geits hide his password. Billy chooses a string S composed of small Latin letters with length L. Then he makes all L-1 one-letter left cyclic shifts of the string and takes as a password one prefix of the lexicographically first of the obtained strings (including S). For example let consider the string alabala. The cyclic one-letter left shifts (including the initial string) are:

alabala
labalaa
abalaal
balaala
alaalab
laalaba
aalabal

and lexicographically first of them is the string aalabal. The first letter of this string is in position 6 in the initial string (the positions in the string are counted from 0).

Write a program that for given string S finds the start position of the smallest lexicographically one-letter left cyclic shift of this string. If the smallest lexicographically left shift appears more than once then the program have to output the smallest initial position.

Your program has to be ready to solve more than one test case. The first line of the input file will contains only the number T of the test cases. Each of the following T lines will describe one test case - first the length L of the string (5 <= L <= 100000) and then, separated by one space, the string S itself.

The output file have to contain exactly T lines with a single number each - the initial position found by your program.


Sample Input

2
6 baabaa
7 alabala


Sample Output

1
6


Source: Southeastern Europe 2003

Submit    Status


Copyright @ 2001-2009, Zhejiang University ACM/ICPC Team, All rights reserved.

 

题意与分析

题意:题意简单,给你一个串,这个串长度固定,可以从不同的位置作为其开始位置,后面的依次加到后面,前面剩余的再依次加到后面,求这个串的这n个串中字典序最小一个在原串中的开始位置。。

分析:将字符串加倍,也就是变成原串的两个连接起来,这样方便处理。然后用后缀数组求出其sa数组和rank数组,然后找尽量下标靠前且满足条件的即可。。。。其实还有一种更加简单的方法,最小表示法,用最小表示法可以直接求出其最小的位置,其代码相当短。。。

源代码

#include<iostream>
using namespace std;
 
const int N=200010;
char s[N];
int len, ans;
 
int __max(int a, int b)
{
        return a > b ? a : b;
}
 
int __min(int a, int b)
{
        return a > b ? b : a;
}
int minp()
{
        int i=0, j=1, k;
        while(j<len && i<len)
        {
               k = 0;
               while(k<len && s[i+k]==s[j+k])
                       k++;
               if(k>=len)
                       break;
               if(s[i+k]>s[j+k])
                       i = __max(i+k+1, j+1);
               else
                       j = __max(j+k+1, i+1);
        }
        return __min(i, j);
}
 
int main()
{
        int t, i;
        scanf("%d", &t);
        while(t--)
        {
               scanf("%d %s", &len, s);
               //len = strlen(s);
               for(i=0; i<len-1; i++)
                       s[i+len] = s[i];
               s[i+len] = '\0';
               ans = minp();
               printf("%d\n", ans);
        }
        return 0;
}
 
/*
//后缀数组套模板780ms
#include<stdio.h>
#include<iostream>
//using namespace std;
 
const int maxn=200010;
 
int wa[maxn], wb[maxn], wv[maxn], ws[maxn];
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 *sa, int n, int m)//n为原来所有个数加1,因为数组最后一般加上一个0,m为一个200左右的基数
{
        int i, j, p, *x=wa, *y=wb, *t;
        for(i=0; i<m; i++) ws[i] = 0;
        for(i=0; i<n; i++) ws[x[i]=r[i]]++;
        for(i=1; i<m; i++) ws[i] += ws[i-1];
        for(i=n-1; i>=0; i--) sa[--ws[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++) ws[i] = 0;
               for(i=0; i<n; i++) ws[wv[i]]++;
               for(i=1; i<m; i++) ws[i]+=ws[i-1];
               for(i=n-1; i>=0; i--) sa[--ws[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++;
        }
}
 
int rank[maxn], height[maxn];
void calheight(int *r, int *sa, int n)//n为数组原来的实际个数
{
        int i, j, k=0;
        for(i=1; i<=n; i++)
               rank[sa[i]] = i;
        for(i=0; i<n; height[rank[i++]]=k)
        {
               k ? k-- : 0;
               for(j=sa[rank[i]-1]; r[i+k]==r[j+k]; k++);
        }
}
 
char s[maxn];
int a[maxn], sa[maxn];
int ii;
 
int main()
{
        int t, n, n1, i, j;
        scanf("%d", &t);
        while(t--)
        {
               scanf("%d %s", &n, s);
               for(i=0; i<n-1; i++)
                       s[i+n] = s[i];
               s[i+n] = '\0';
               n1 = 2*n-1;
               for(i=0; i<n1; i++)
                       a[i] = s[i] - 'a' + 1;
               a[n1] = 0;
               da(a, sa, n1+1, 200);
               calheight(a, sa, n1);
 
               for(i=1; i<=n1; i++)
                       if(n1-sa[i]>=n)
                               break;
               for(j=i+1; j<=n1&&height[j]>=n; j++)
                       if(sa[j]<sa[i])
                               i = j;
                               
               printf("%d\n", sa[i]);
        }
 
        return 0;
}*/

                                            

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值