hdu 5442 2015长春网络赛

Favorite Donut

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 478    Accepted Submission(s): 126


Problem Description
Lulu has a sweet tooth. Her favorite food is ring donut. Everyday she buys a ring donut from the same bakery. A ring donut is consists of  n  parts. Every part has its own sugariness that can be expressed by a letter from  a  to  z  (from low to high), and a ring donut can be expressed by a string whose i-th character represents the sugariness of the  ith  part in clockwise order. Note that  z  is the sweetest, and two parts are equally sweet if they have the same sugariness.

Once Lulu eats a part of the donut, she must continue to eat its uneaten adjacent part until all parts are eaten. Therefore, she has to eat either clockwise or counter-clockwise after her first bite, and there are  2n  ways to eat the ring donut of  n  parts. For example, Lulu has  6  ways to eat a ring donut  abc abc,bca,cab,acb,bac,cba . Lulu likes eating the sweetest part first, so she actually prefer the way of the greatest lexicographic order. If there are two or more lexicographic maxima, then she will prefer the way whose starting part has the minimum index in clockwise order. If two ways start at the same part, then she will prefer eating the donut in clockwise order. Please compute the way to eat the donut she likes most.
 

Input
First line contain one integer  T,T20 , which means the number of test case.

For each test case, the first line contains one integer  n,n20000 , which represents how many parts the ring donut has. The next line contains a string consisted of  n  lowercase alphabets representing the ring donut.
 

Output
You should print one line for each test case, consisted of two integers, which represents the starting point (from  1  to  n ) and the direction ( 0  for clockwise and  1  for counterclockwise).
 

Sample Input
  
  
2 4 abab 4 aaab
 

Sample Output
  
  
2 0 4 0
 

Source
 

Recommend
hujie   |   We have carefully selected several similar problems for you:   5449  5448  5447  5446  5445 

将原串复制一遍,加上一个比出现的字符都小的字符,翻转原串在复制一遍,构成一个长度为4*len+1的串,这里将四个组成的串称为1 2 3 4串。

维护出后缀数组,然后对sa数组从后往前扫描,每扫描出一个1串或者3串的后缀,就更新一下当前的最左的位置还有顺时针还是逆时针走。

枚举直到当前后缀与排名前一名的后缀的最长公共前缀小于len。

比赛时晚到了。。。很着急。。。一直没做出来。。哎,还是太弱

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <stack>

using namespace std;

#define INF 110000
#define N 201000
#define M 201000

int k, n, m, len1, len2;
char s[N];
int str[N], str2[N], ran[N], sa[N], bucket[N], trank[N], sa2[N], height[N];

int cmp(int p1, int p2, int len)
{
    return p1+len < n && p2+len < n && trank[p1] == trank[p2] && trank[p1+len] == trank[p2+len];
}

void getSa(int* str)
{
    m = 300;
    for(int i = 0; i < m; i++) bucket[i] = 0;
    for(int i = 0; i < n; i++) bucket[ran[i] = str[i]]++;
    for(int i = 1; i < m; i++) bucket[i] += bucket[i-1];
    for(int i = n-1; i >= 0; i--) sa[--bucket[ran[i]]] = i;
    for(int j = 1, p = 0; p < n; j <<= 1, m = p)
    {
        p = 0;
        for(int i = n-j; i < n; i++) sa2[p++] = i;
        for(int i = 0; i < n; i++)
            if(sa[i] >= j) sa2[p++] = sa[i]-j;
        for(int i = 0; i < m; i++) bucket[i] = 0;
        for(int i = 0; i < n; i++) bucket[ran[i]]++;
        for(int i = 1; i < m; i++) bucket[i] += bucket[i-1];
        for(int i = n-1; i >= 0; i--) sa[--bucket[ran[sa2[i]]]] = sa2[i];
        for(int i = 0; i < n; i++) trank[i] = ran[i];
        p = 0; ran[sa[0]] = p++;
        for(int i = 1; i < n; i++)
            ran[sa[i]] = cmp(sa[i], sa[i-1], j) ? p-1 : p++;
    }
}
void getHeight()
{
    int h = 0;
    height[0] = 0;
    for(int i = 0; i < n; i++)
    {
        if(ran[i] == 0) continue;
        if(h) h--;
        int pre = sa[ran[i]-1];
        for(; pre+h < n && i+h < n && str[i+h] == str[pre+h]; h++);
        height[ran[i]] = h;
    }
}

int main()
{
    //freopen("C:\\Users\\zfh\\Desktop\\in.txt", "r", stdin);
    int t; scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        scanf(" %s", s);
        int cnt = 0;
        for(int i = 0; i < n; i++)
        {
            str[cnt++] = s[i];
        }
        for(int i = 0; i < n; i++)
        {
            str[cnt++] = s[i];
        }
        str[cnt++] = 0;
        for(int i = 0; i < n; i++)
            str[cnt++] = s[n-i-1];
        for(int i = 0; i < n; i++)
            str[cnt++] = s[n-i-1];
        int len = n;
        n = cnt;
        getSa(str);
        getHeight();
        int pos = len+1, dir;
        for(int i = n-1; i >= 0; i--)
        {
            if(sa[i] < len)
            {
                int temp = sa[i]+1;
                if(temp < pos)
                {
                    pos = temp;
                    dir = 0;
                }
                else if(temp == pos)
                    dir = 0;
            }
            else if(sa[i] >= 2*len+1 && sa[i] <= 3*len)
            {
                int temp = len-(sa[i]-2*len)+1;
                if(temp < pos)
                {
                    pos = temp;
                    dir = 1;
                }
            }
            if(height[i] < len) break;
        }

        printf("%d %d\n", pos, dir);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值