Favorite Donut

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5442

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 i−th 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,T≤20, which means the number of test case.

For each test case, the first line contains one integer n,n≤20000, 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

HINT

题意

给你一个环,环上的字符表示甜的程度,你可以正着吃,也可以反着吃

然后问你怎么吃可以使得甜度最大

题解:

后缀数组,正反各跑一遍,然后拿到俩长度为n的最大的字符串

然后再o(n)比一下就好了

正解其实应该是最小表示法,十几行就可以解决,O(n)

大概后缀自动机也是O(n)的算法也可以搞定~

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 50000
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** int s[][maxn];
int n,nn;
int sa[maxn], rk[maxn], height[maxn];
int wa[maxn], wb[maxn], wv[maxn], wd[maxn]; int cmp(int *r, int a, int b, int l){
return r[a] == r[b] && r[a+l] == r[b+l];
} void build_sa(int *r, int n, int m){
int i, j, p, *x = wa, *y = wb, *t;
for(i = ; i < m; i ++) wd[i] = ;
for(i = ; i < n; i ++) wd[x[i]=r[i]] ++;
for(i = ; i < m; i ++) wd[i] += wd[i-];
for(i = n-; i >= ; i --) sa[-- wd[x[i]]] = i;
for(j = , p = ; p < n; j *= , m = p){
for(p = , i = n-j; i < n; i ++) y[p ++] = i;
for(i = ; i < n; i ++) if(sa[i] >= j) y[p ++] = sa[i] - j;
for(i = ; i < n; i ++) wv[i] = x[y[i]];
for(i = ; i < m; i ++) wd[i] = ;
for(i = ; i < n; i ++) wd[wv[i]] ++;
for(i = ; i < m; i ++) wd[i] += wd[i-];
for(i = n-; i >= ; i --) sa[-- wd[wv[i]]] = y[i];
for(t = x, x = y, y = t, p = , x[sa[]] = , i = ; i < n; i ++){
x[sa[i]] = cmp(y, sa[i-], sa[i], j) ? p - : p ++;
}
}
} void calHeight(int *r, int n){
int i, j, k = ;
for(i = ; i <= n; i ++) rk[sa[i]] = i;
for(i = ; i < n; height[rk[i ++]] = k){
for(k ? k -- : , j = sa[rk[i]-]; r[i+k] == r[j+k]; k ++);
}
}
void init()
{
memset(sa,,sizeof(sa));
memset(rk,,sizeof(rk));
memset(height,,sizeof(height));
memset(wa,,sizeof(wa));
memset(wb,,sizeof(wb));
memset(wv,,sizeof(wv));
memset(wd,,sizeof(wd));
}
bool cmpp(int x,int y)
{
for(int i=;i<n;i++)
{
if(s[][x+i]!=s[][y+i])
{
return s[][x+i]>s[][y+i];
}
}
return x<=n-y-;
}
int main()
{
int T;
scanf("%d",&T);
char str[maxn];
while(T--)
{
int f1,f2;
scanf("%d",&n);
scanf("%s",str);
init();
for(int i=;i<n;i++) s[][i]=s[][i+n]=(int)str[i];
nn=n<<;
s[][nn]=;
build_sa(s[],nn+,);
for(int i=nn;i>=;i--)
{
if(sa[i]<n)
{
f1=sa[i];
break;
}
}
init();
for(int i=;i<n;i++) s[][i]=s[][i+n]=(int)str[n-i-];
s[][nn]=;
build_sa(s[],nn+,);
calHeight(s[],nn);
int mx=n;
f2=-;
// for(int i=1;i<=nn;i++) cout<<i<<" "<<sa[i]<<" "<<height[i]<<endl;
for(int i=nn;i>=;i--)
{
if(f2!=-) mx=min(mx,height[i+]);
if(mx<n) break;
if(sa[i]<n)
{
f2=sa[i];
}
}
// cout<<f1<<" "<<f2<<endl;
if(cmpp(f1,f2))
{
printf("%d 0\n",f1+);
}
else
{
printf("%d 1\n",n-f2);
}
}
}

hdu 5442 Favorite Donut 后缀数组的更多相关文章

  1. Hdu 5442 Favorite Donut (2015 ACM/ICPC Asia Regional Changchun Online 最大最小表示法 + KMP)

    题目链接: Hdu 5442 Favorite Donut 题目描述: 给出一个文本串,找出顺时针或者逆时针循环旋转后,字典序最大的那个字符串,字典序最大的字符串如果有多个,就输出下标最小的那个,如果 ...

  2. HDU 5442 Favorite Donut(暴力 or 后缀数组 or 最大表示法)

    http://acm.hdu.edu.cn/showproblem.php?pid=5442 题意:给出一串字符串,它是循环的,现在要选定一个起点,使得该字符串字典序最大(顺时针和逆时针均可),如果有 ...

  3. hdu 4622 Reincarnation(后缀数组)

    hdu 4622 Reincarnation 题意:还是比较容易理解,给出一个字符串,最长2000,q个询问,每次询问[l,r]区间内有多少个不同的字串. (为了与论文解释统一,这里解题思路里sa数组 ...

  4. HDU 5769 Substring(后缀数组)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5769 [题目大意] 在一个串中求出包含字母的子串个数, 只要存在一个字符不相等的子串即可视为不同的 ...

  5. HDU 4691 正解后缀数组(暴力也能过)

    本来是个后缀数组,考察算法的中级题目,暴力居然也可以水过,就看你跳不跳坑了(c++和G++返回结果就很不一样,关键看编译器) 丝毫不差的代码,就看运气如何了.唯一差别c++还是G++,但正解是后缀数组 ...

  6. hdu 3518 Boring counting 后缀数组LCP

    题目链接 题意:给定长度为n(n <= 1000)的只含小写字母的字符串,问字符串子串不重叠出现最少两次的不同子串个数; input: aaaa ababcabb aaaaaa # output ...

  7. HDU - 6704 K-th occurrence (后缀数组+主席树/后缀自动机+线段树合并+倍增)

    题意:给你一个长度为n的字符串和m组询问,每组询问给出l,r,k,求s[l,r]的第k次出现的左端点. 解法一: 求出后缀数组,按照排名建主席树,对于每组询问二分或倍增找出主席树上所对应的的左右端点, ...

  8. hdu5442 Favorite Donut 后缀数组 长春网赛

    wa从一点到晚上11点没停过,也不知道为什么错,第二天换了个思路做,终于过了.这题还是有点问题的,数据有点水,我看到有人贴的代码baabbaab这组数据是4 0,明显错的,但是却可以过. 下面的是我第 ...

  9. HDU 5442——Favorite Donut——————【最大表示法+kmp | 后缀数组】

    Favorite Donut Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

随机推荐

  1. 中国VR公司的详尽名单

    中国VR公司的详尽名单   <VR圈深度投资报告一:2014年以来所有VR/AR融资事件> 特征一.投资机构观望居多 尽管VR在媒体和二级市场炒得很热,但大多风险投资机构却慎于出手,以观望 ...

  2. UNICODE,GBK,UTF-8区别

    简单来说,unicode,gbk和大五码就是编码的值,而utf-8,uft-16之类就是这个值的表现形式.而前面那三种编码是一兼容的,同一个汉字,那三个码值是完全不一样的.如"汉"的uncode值与g ...

  3. UVa 11572 (滑动窗口) Unique Snowflakes

    滑动窗口挺有意思的,如果符合条件右端点一直向前走,不符合的话,左端点向前走. #include <bits/stdc++.h> using namespace std; set<in ...

  4. SQL SERVER 2000 & SQL SERVER 2005 数据缓存依赖

    一.SQL SERVER 7.0/2000和SQL SERVER 2005的简介及比较 1.1     SQL SERVER 7.0/2000 SQL SERVER 7.0/2000没有提供内置的支持 ...

  5. Spring事务隔离级别和传播特性

    相信每个人都被问过无数次Spring声明式事务的隔离级别和传播机制吧!今天我也来说说这两个东西. 加入一个小插曲, 一天电话里有人问我声明式事务隔离级别有哪几种, 我就回答了7种, 他问我Spring ...

  6. Imageview使用记录

    1. imageView清除背景 原文网址:http://blog.csdn.net/lzq1039602600/article/details/40393591 两种清除 imageView的背景 ...

  7. Spring基础知识及bean的配置

    IOC与DI: IOC(inversion of control):其思想是反转资源获取的方向.传统的资源查找方式要求组件向容器发起请求查找资源.作为回应,容器适时的返回资源.而应用了IOC之后,则是 ...

  8. 将你的Asp.NET应用程序嵌入到SharePoint

    转:http://www.cnblogs.com/Clank/archive/2007/05/21/754073.html 为什么要将Asp.net应用程序嵌入到SharePoint?这个我们不讨论! ...

  9. android之APN

    APN全称是Access Point Name,中文即接入点,是通过手机上网时必须配置的一个参数,它决定了手机通过哪种接入方式来访问网络. android系统把所有的APN都保存在数据库中,数据库绝对 ...

  10. 命令cp

    cp 源 目标cp -r  递归复制整个目录cp -v  显示复制的详细信息