hdu 5442 Favorite Donut(最大表示法+kmp)

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 nn parts. Every part has its own sugariness that can be expressed by a letter from aa to zz (from low to high), and a ring donut can be expressed by a string whose i-th character represents the sugariness of the i−thi−th part in clockwise order. Note that zz 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 2n2n ways to eat the ring donut of nn parts. For example, Lulu has 66 ways to eat a ring donut abcabc: abc,bca,cab,acb,bac,cbaabc,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,T≤20T,T≤20, which means the number of test case.

For each test case, the first line contains one integer n,n≤20000n,n≤20000, which represents how many parts the ring donut has. The next line contains a string consisted of nn 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 11 to nn) and the direction (00 for clockwise and 11 for counterclockwise).
Sample Input
2
4
abab
4
aaab
Sample Output
2 0
4 0

題意非常不清楚

給你一串字符串 可以順時針,也可以逆時針,讓你找出最大表示法,
如果最大表示相同,輸出順時針的
否則輸出 最大表示同時轉的次數最小的方向。

把一個串拼接兩次,就可以得到環上的所有情況,順時針直接得就好,逆時針,翻轉後,找到最大,這樣得到的是最後的位置(因爲翻轉了的緣故,可以模擬看看,翻轉後,可以得到逆序的所有序列,但是順序也會調換),用kmp找到最後完整出現的位置就好

#include <bits/stdc++.h>
using namespace std;

const int maxn = 4e5;
int a[maxn],b[maxn],pos[maxn];
int nxt[maxn],c[maxn];
int getMax(int a[], int n)
{
    int i, j, k;
    for(i = 0, j = 1; i<n&&j<n; ) {
        for(k = 0; k<n&&a[i+k]==a[j+k]; k++)
            ;
        if(k>=n)
            break;
        if(a[i+k]>a[j+k])
            j += k+1;
        else
            i += k+1;
        if(i == j)
            j++;
    }
    return i;
}

int solve(string s,int a[],int n)
{
    for(int i=0;i<n;i++)
        a[i]=s[i]-'a';
    for(int i=n;i<2*n-1;i++)
        a[i]=a[i-n];
    int po=getMax(a,n);
    return po;
}

void init_kmp(int m) {
    int i = 0, j = -1;
    nxt[0] = -1;
    while(i<m) {
        if(j == -1 || c[i] == c[j]) {
            ++i, ++j;
            nxt[i] = j;
        } else {
            j = nxt[j];
        }
    }
}
int kmp(int n)
{
    int cnt = 0;
    int i = 0, j = 0;
    while(i<2*n-1) {
        if(j == -1 || b[i] == c[j]) {
            i++, j++;
        } else {
            j = nxt[j];
        }
        if(j == n) {
            pos[cnt++] = i;
            j = nxt[j];
        }
    }
    return cnt;
}

int main(){
    int t,n;
    string s;
    scanf("%d",&t);
    while(t--)
    {
        cin>>n>>s;
        int pos1=solve(s,a,n);
        int num=0,flag=-1;
        reverse(s.begin(),s.end());
        int pos2=solve(s,b,n);
        int tmp=pos1,tmp2=pos2;
        while(num<n)
        {
            if(a[pos1]>b[pos2]) {
                flag=1;
                break;
            }
            else if(a[pos1]<b[pos2])
            {
                flag=0;
                break;
            }
            else {
                pos1++;
                pos2++;
                num++;
            }
        }
        int cnt=0;      
        for(int i=tmp2;i<tmp2+n;i++)
            c[cnt++]=b[i];
        init_kmp(n);
        cnt=kmp(n);
        tmp2=pos[cnt-1]%n;
        if(num==n)
        {

            if(tmp+1<=n-tmp2)
                printf("%d 0\n",tmp+1 );
            else printf("%d 1\n",n-tmp2 );
        }
        else {
            if(flag)
                printf("%d 0\n",tmp+1 );
            else printf("%d 1\n",n-tmp2 );
        }
    }
}
發佈了60 篇原創文章 · 獲贊 4 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章