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的字典序最大的串 ...
随机推荐
- 模拟鼠标向下滚动 http://bbs.2ccc.com/topic.asp?topicid=461769
我想模拟鼠标滚轮,下面的代码能向上滚动,怎么样下向滚动啊 mouse_event( MOUSEEVENTF_WHEEL,0,0,WHEEL_DELTA,0); 我把参数设置为mouse_event( ...
- window.open弹窗阻止问题解决之道
https://segmentfault.com/a/1190000015381923https://segmentfault.com/a/1190000014988094https://www.cn ...
- MySQL 编码:utf8 与 utf8mb4,utf8mb4_unicode_ci 与 utf8mb4_general_ci
参考:mysql字符集小结 utf8mb4 已成为 MySQL 8.0 的默认字符集,在MySQL 8.0.1及更高版本中将 utf8mb4_0900_ai_ci 作为默认排序规则. 新项目只考虑 u ...
- spring map获取同类型的bean
今天看博客怎么减少if else 方法, 才发现spring 还有很多功能我没有用到,以后真的得花时间学学spring,今天学到的东西如下: 1.定义一个接口 store public interfa ...
- FormData模拟表单上传图片
[node]---multer模块实现图片上传---FORMDATA 1.安装muterl第三方模块 cnpm install multer --save 2.使用 multer在解析完成后,会向 ...
- 什么是HIS、PACS、LIS、RIS
什么是HIS?医院信息系统的定义(HIS)医院信息系统(Hospital Information System,HIS)在国际学术界已公认为新兴的医学信息学(Medical Informatics)的 ...
- 16 (H5*) JS第6天 日期、字符串、数组
目录 1:date 2:格式化日期 3:字符串 4:字符串的方法 5:字符串的案例 6:数组的方法 7:包装类型 复习 正文 1:日期:date <script> //创建实例对象 // ...
- STL 配接器(adapters)
定义 配接器(adapters):将一个class的接口,转换为另一个class的接口,使得原来不能一起使用相互兼容的classes,可以一起协同工作. 配接器是一种设计模式. STL中提供的各种配接 ...
- SpringBoot 使用logback
1.添加pom引用 <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback ...
- Spring源码解析-核心类之XmlBeanFactory 、DefaultListableBeanFactory
DefaultListableBeanFactory XmlBeanFactory 继承自 DefaultListableBeanFactory , 而 DefaultListableBeanFact ...