HDOJ 5442 Favorite Donut

Favorite Donut

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


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

最小表示法 +KMP

#include <cstdio>
#include <cstring>

const int N = 20010;

int get_min(char *str, int len) {
	int i, j, k;
	i = k = 0, j = 1;
	while (i < len && j < len && k < len) {
		int t = str[(i + k) % len] - str[(j + k) % len];
		if (t == 0)
			++k;
		else {
			if (t < 0) i += k + 1;
			else j += k + 1;
			if (i == j) ++j;
			k = 0;
		}
	}
	return i < j ? i : j;
}

void change(char* S1, char* S2, int len) {
	for (int i = 0; i <= len; ++i)
		S2[i] = S1[len - i - 1];
}

void get_next(char *str, int *next) {
	int i = 0, j = -1;
	next[i] = j;

	while (str[i]) {
		if (j == -1 || str[i] == str[j])
			next[++i] = ++j;
		else
			j = next[j];
	}
}

int main() {
	int T, len;
	char S1[N], S2[N];
	int next[N];

	scanf("%d", &T);
	while (T--) {
		scanf("%d%s", &len, S1);
		change(S1, S2, len);

		int x = get_min(S1, len);
		int y = get_min(S2, len);
		get_next(S2, next);

		int t = len - next[len];
		if (t && t != len && (len % t == 0)) {
			y += len - t;
		}

		int flag = 0;
		int k;
		for (k = 0; k < len; ++k) {
			if (S1[(x + k) % len] > S2[(y + k) % len])
				break;
			else if (S1[(x + k) % len] < S2[(y + k) % len]) {
				flag = 1;
				break;
			}
		}
		y = len - y;
		++x;
		if (k < len) {
			if (flag == 1)
				printf("%d 1\n", y);
			else
				printf("%d 0\n", x);

		}
		else {
			if (x <= y)
				printf("%d 0\n", x);
			else
				printf("%d 1\n", y);
		}
	}

	return 0;
}

后缀数组

#include <cstdio>
#include <cstring>

const int N = 51000;

int wa[N], wb[N], wv[N], ws[N];
char S1[N], S2[N], TT[N];
int sa[N];
int len;

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

void DA(char* ary, int n, int m) {
    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] = ary[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 = p = 1; p < n; m = p, j *= 2) {
        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];

        t = x, x = y, y = t;
        for (x[sa[0]] = 0, p = i = 1; i < n; ++i)
            x[sa[i]] = Cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++;
    }
}

void change() {
    for (int i = 0; i < len * 2; ++i)
        S2[i] = S1[len * 2 - i - 1];
    S2[len * 2] = 'z' + 1;
    S2[len * 2 + 1] = 0;
}

int main() {
    int T;
    scanf("%d", &T);

    for (int xx = 0; xx < T; ++xx) {
        int x1, x2;
        scanf("%d%s", &len, S1);
        strcpy(TT, S1);
        strcat(S1, TT);
        DA(S1, len * 2 + 1, 300);
        for (int i = len * 2; i >= 0; --i) {
            if (sa[i] < len) {
                x1 = sa[i];
                break;
            }
        }

        change();
        DA(S2, len * 2 + 2, 300);
        for (int i = len * 2; i >= 0; --i) {
            if (sa[i] < len) {
                x2 = sa[i];
                break;
            }
        }

        int flag = 0;
		int k;
        for (k = 0; k < len; ++k) {
            if (S1[x1 + k] > S2[x2 + k]) 
				break;
            else if (S1[x1 + k] < S2[x2 + k]) {
                flag = 1;
                break;
            }
        }
		x2 = len - x2;
        ++x1;


        if (k < len) {
            if (flag == 0)
                printf("%d %d\n", x1, 0);
            else
                printf("%d %d\n", x2, 1);
        }
        else {
            if (x1 <= x2)
                printf("%d %d\n", x1, 0);
            else
                printf("%d %d\n", x2, 1);
        }

    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值