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. JS-在本页面禁止页面返回

    这个问题是最近遇到的 解决方案我百度的并测试有效 // 如果你希望用户不用有返回功能 可缩写如下 或使用location.replace('url')跳转链接 history.pushState(nu ...

  2. source ~/.bash_profile是什么意思

    ~ 这个符号表示你的家目录,.bash_profile 是一个隐藏文件,主要是用来配置bash shell的,source -/.bash_profile 就是让这个配置文件在修改后立即生效.

  3. SpringBoot整合jsp技术

    1.修改pom.xml文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...

  4. Learn Python the hard way, ex41 来自Percal 25 号星星的哥顿人

    我承认,我偷懒了,少打了大量代码(剧情),英文太差,下次可以编个中文的试试 #!/urs/bin/python #coding:utf-8 from sys import exit from rand ...

  5. Value Iteration Algorithm for MDP

    Value-Iteration Algorithm: For each iteration k+1: a. calculate the optimal state-value function for ...

  6. 20191103 《Spring5高级编程》笔记-第4章

    第4章 详述Spring配置和Spring Boot 4.2 管理bean生命周期 通常,有两个生命周期事件与bean特别相关:post-initialization和pre-destruction. ...

  7. Nginx配置之rewrite、proxy_pass、upstream、location

    如图,这是Nginx的配置文件nginx.conf中的一段配置代码. 在http段中定义了一个名为webservers的upstream模块,主要用于负载均衡. 在server模块中,定义了一个loc ...

  8. 中国剩余定理(CRT) & 扩展中国剩余定理(ExCRT)总结

    中国剩余定理(CRT) & 扩展中国剩余定理(ExCRT)总结 标签:数学方法--数论 阅读体验:https://zybuluo.com/Junlier/note/1300035 前置浅讲 前 ...

  9. 什么是 Python?

     Python 是一种编程语言,它有对象.模块.线程.异常处理和自动内存管理,可以加入其他语言的对比.  Python 是一种解释型语言,Python 在代码运行之前不需要解释.  Python 是动 ...

  10. centos解决Could not find a version that satisfies the requirement pip3 (from versions: none)及No matching distribution found for pip3问题

    python环境:python 3.8 报错信息: WARNING: pip is configured with locations that require TLS/SSL, however th ...