Favorite Donut

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

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 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
 
Source

题意:Lulu喜欢吃炸面圈,炸面圈有n个部分组成,每个部分由小写字母代表其甜度,Lulu总是从最甜的开始吃,两个方向,哪个最甜吃哪个。输出满足她要求吃法的最开始的下标,和方向。最大最小表示法

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm> using namespace std; #define maxn 40008 int n, Next[maxn]; void Getnext(char s[], int n) // kmp获得Next数组,next会出现编译错误,我也不造为什么……
{
int j, k;
j = ;
k = Next[] = -;
while(j < n)
{
if(k == - || s[j] == s[k]) Next[++j] = ++k;
else
k = Next[k];
}
} int GETNEXT(char s[], int n) // 找下标最小的开始的串满足吃法
{
int i, j;
i = ;
j = ;
while(i < n && j < n) // i, j比较,每次让相对字典序小的改变,最后肯定有一个超过n,那个没超过n的就是满足题意的
{
int k = ;
while( s[i+k] == s[j+k] && k < n) // 如果这两个下标对应的字典序相等,继续向下比较,知道不等为止
k++;
if(k == n)
break;
if(s[i+k] > s[j+k])
{
if(j+k > i)
j = j + k + ;
else
j = i + ;
}
else
{
if(i+k > j)
i = i + k + ;
else
i = j + ;
}
}
return min(i, j);
} int KMP(char s[], char s1[], int n, int m) //在逆串中找开始下标最后边的满足题意的串(就是下标最小的,next, kmp优化
{
int i, j, ans;
i = j = ;
while(j < m)
{
while( i == - || (s[i] == s1[j] && j < m))
i++, j++; if(i == n)
ans = j - n;
i = Next[i];
}
return ans;
} int main()
{
int c;
scanf("%d", &c);
char s[maxn], s1[maxn], s2[maxn], s3[maxn], s4[maxn]; while(c--)
{
memset(s, , sizeof(s));
memset(s1, , sizeof(s1));
memset(s2, , sizeof(s2));
memset(s3, , sizeof(s3));
memset(s4, , sizeof(s4)); scanf("%d", &n);
scanf("%s", s);
strcpy(s1, s);
strcat(s1, s); int a = GETNEXT(s1, n); // 找到正序的满足题意的下标
strncpy(s3, s1+a, n); // 把满足题意的s3中 strrev(s);
strcpy(s2, s);
strcat(s2, s);
int b = GETNEXT(s2, n); // 找逆串中的满足题意的下标
strncpy(s4, s2+b, n); Getnext(s4, n);
b = KMP(s4, s2, n, *n-); // 找逆串中满足题意的最后边的下标,也就是正串中最前边的下标……
strncpy(s4, s2+b, n); int g = strcmp(s3, s4); if(g == )
{
if(a+ <= n-b)
printf("%d 0\n", a+);
else
printf("%d 1\n", n-b);
}
else if(g > )
printf("%d 0\n", a+);
else
printf("%d 1\n", n-b);
}
return ;
}
/*
4 4
aaab
4
abca
6
abcabc
4
abab
*/

你已经得到最大表示法的下标时,你也可以这么取满足该串的最小下标……

求逆串的最大最小表示法的“最小下标的时候可以用最小循环节做

 int solve (char* s){
getNext(s);
if (n % (n - Next[n]))
return n;
else
return n - Next[n];
}
int b=Getmin(s);
int k=solve(s);
b = (n - b - ) % k; // 获得最小的……最小循环节是个神奇的东西……

Favorite Donut的更多相关文章

  1. 使用Donut Caching和Donut Hole Caching在ASP.NET MVC应用中缓存页面

    Donut Caching是缓存除了部分内容以外的整个页面的最好的方式,在它出现之前,我们使用"输出缓存"来缓存整个页面. 何时使用Donut Caching 假设你有一个应用程序 ...

  2. hdu 5442 Favorite Donut 后缀数组

    Favorite Donut Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid ...

  3. 使用 angular directive 和 json 数据 D3 随着标签 donut chart演示样本

    使用angular resource载入中priorityData.json中间json数据,结合D3绘制甜甜圈图.执行index.html其结果见于图.: priorityData.json中jso ...

  4. ASP.NET MVC 缓存扩展 - Donut Caching

    项目介绍 ASP.NET MVC Extensible Donut Caching brings donut caching to ASP.NET MVC 3 and later. The code ...

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

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

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

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

  7. HDU 5442 Favorite Donut

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

  8. Msf+Donut执行任意可执行文件

    地址:donut:https://github.com/TheWover/donut 介绍:是一个shellcode生成工具,可以将.NET程序集等转换为shellcode. 使用:donut.exe ...

  9. hdu5442 Favorite Donut

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5442 题目大意:给你一个长度为n的字符串,将它首尾相连成环.问你这个环上找一个长度为n的字典序最大的串 ...

随机推荐

  1. struts2 基础5 OGNL、标签、四大域、默认拦截器说明

    OGNL表达式 OGNL:对象导抗图语言 OGNL表达式是一个上下文的概念,上下文Map结构 OGNL表达式需要使用#标注命名空间.访问上下文(Context)中的对象需要使用#符号标注命名空间,如# ...

  2. Vue Router 路由守卫:完整的导航解析流程

    完整的导航解析流程 1 导航被触发. 2 在失活的组件里调用离开守卫. 3 调用全局的 beforeEach 守卫. 4 在重用的组件里调用 beforeRouteUpdate 守卫 (2.2+). ...

  3. Java ——String 类 StringBuffer 和 StringBuilder类 随机字符

    本节重点思维导图 String 类 创建字符串 String str = "I love ai"; 在代码中遇到字符串常量时,"I love ai",编译器会使 ...

  4. 应用安全-CMF/CMS漏洞整理

    CMS识别 云悉 http://whatweb.bugscaner.com/batch.html CakePHP  CakePHP <= / Cache Corruption Exploit 2 ...

  5. Oracle数据库的特点与工作原理

    Oracle数据库的特点 1.开放性: Oracle能在所有主流平台上运行(包括Windows),完全支持所有的工业标准,采用完全开放策略,可以使客户选择最适合的解决方案,对开发商全力支持. 2.可伸 ...

  6. K-th Number Poj - 2104 主席树

    K-th Number Poj - 2104 主席树 题意 给你n数字,然后有m次询问,询问一段区间内的第k小的数. 解题思路 这个题是限时训练做的题,我不会,看到这个题我开始是拒绝的,虽然题意清晰简 ...

  7. [AHOI2013]作业 (莫队+分块)

    [AHOI2013]作业 (莫队+分块) 题面 给定了一个长度为n的数列和若干个询问,每个询问是关于数列的区间[l,r],首先你要统计该区间内大于等于a,小于等于b的数的个数,其次是所有大于等于a,小 ...

  8. jQuery淡入淡出瀑布流效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. redis 安装和运行

    安装redis 在CentOs虚拟机上:yum install redis,可能会出现以下错误: 解决方式:先输入yum install epel-release,之后输入:redis-server启 ...

  10. React框架新闻网站学习过程中遇到的错误总结

    1.安装指定版本插件命令 npm install 插件名字@1.1.4(版本号) --save 或 yarn add 插件名字@1.1.4(版本号) --dev 2.Error:‘Link’ is n ...