Favorite Donut
Favorite Donut
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1378 Accepted Submission(s): 356
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.
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.
题意: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的更多相关文章
- 使用Donut Caching和Donut Hole Caching在ASP.NET MVC应用中缓存页面
Donut Caching是缓存除了部分内容以外的整个页面的最好的方式,在它出现之前,我们使用"输出缓存"来缓存整个页面. 何时使用Donut Caching 假设你有一个应用程序 ...
- hdu 5442 Favorite Donut 后缀数组
Favorite Donut Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid ...
- 使用 angular directive 和 json 数据 D3 随着标签 donut chart演示样本
使用angular resource载入中priorityData.json中间json数据,结合D3绘制甜甜圈图.执行index.html其结果见于图.: priorityData.json中jso ...
- ASP.NET MVC 缓存扩展 - Donut Caching
项目介绍 ASP.NET MVC Extensible Donut Caching brings donut caching to ASP.NET MVC 3 and later. The code ...
- HDU 5442——Favorite Donut——————【最大表示法+kmp | 后缀数组】
Favorite Donut Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- Hdu 5442 Favorite Donut (2015 ACM/ICPC Asia Regional Changchun Online 最大最小表示法 + KMP)
题目链接: Hdu 5442 Favorite Donut 题目描述: 给出一个文本串,找出顺时针或者逆时针循环旋转后,字典序最大的那个字符串,字典序最大的字符串如果有多个,就输出下标最小的那个,如果 ...
- HDU 5442 Favorite Donut
Favorite Donut Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- Msf+Donut执行任意可执行文件
地址:donut:https://github.com/TheWover/donut 介绍:是一个shellcode生成工具,可以将.NET程序集等转换为shellcode. 使用:donut.exe ...
- hdu5442 Favorite Donut
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5442 题目大意:给你一个长度为n的字符串,将它首尾相连成环.问你这个环上找一个长度为n的字典序最大的串 ...
随机推荐
- netcore2.1 在后台运行一个任务
在 ASP.NET Core 2.1中, 提供了一个名为BackgroundService的类,在 Microsoft.Extensions.Hosting命名空间中,其代码为 namespace M ...
- Jmeter的中英文互换
1.jmeter的中英文互换:为了更深入的了解Jmeter,一般使用英文版的jmeter. 旧版本下载的默认中文较多.新版本5.1的下载后默认英文比较的多. 方法一: 选项->选择一种语言,但是 ...
- 【HANA系列】SAP HANA快捷键大全
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA快捷键大全 ...
- js数组,运算符
- Spring Security框架入门
1.Spring Security框架入门 1.1 Spring Security简介 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框 ...
- 异步json发送put或者delete
第一种 put请求或者delete请求 直接写发送的情况 //批量删除 function batchDel() { var ids = []; $("#list-table").f ...
- [POI2006]ORK-Ploughing(贪心,枚举)
[POI2006]ORK-Ploughing 题目描述 Byteasar, the farmer, wants to plough his rectangular field. He can begi ...
- #10042. 「一本通 2.1 练习 8」收集雪花 || 离散化 || 双指针法 || C++ || LOJ
题目:#10042. 「一本通 2.1 练习 8」收集雪花 看到网上没有这道题的题解,所以写一下. 要标记数字是否存在,看到x<=1e9,所以考虑用离散化,然后开一个last数组,last[i] ...
- 10 | MySQL为什么有时候会选错索引? 学习记录
<MySQL实战45讲>10 | MySQL为什么有时候会选错索引? 学习记录http://naotu.baidu.com/file/e7c521276650e80fe24584bc9a6 ...
- Java 根据银行卡号获取银行名称以及图标
转 https://blog.csdn.net/N_007/article/details/78835526 参考 CNBankCard 中国各大银行卡号查询 一.支付宝接口获取名称 根据 卡号 获取 ...