hdu 5442 Favorite Donut(最大表示法+kmp)
来源:互联网 发布:开淘宝店怎么找厂家 编辑:程序博客网 时间:2024/05/20 23:38
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 nn parts. Every part has its own sugariness that can be expressed by a letter from aa to zz (from low to high), and a ring donut can be expressed by a string whose i-th character represents the sugariness of the i−thi−th part in clockwise order. Note that zz 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 2n2n ways to eat the ring donut of nn parts. For example, Lulu has 66 ways to eat a ring donut abcabc: abc,bca,cab,acb,bac,cbaabc,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≤20T,T≤20, which means the number of test case.
For each test case, the first line contains one integer n,n≤20000n,n≤20000, which represents how many parts the ring donut has. The next line contains a string consisted of nn 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 11 to nn) and the direction (00 for clockwise and 11 for counterclockwise).
Sample Input
2
4
abab
4
aaab
Sample Output
2 0
4 0
题意非常不清楚
给你一串字符串 可以顺时针,也可以逆时针,让你找出最大表示法,
如果最大表示相同,输出顺时针的
否则输出 最大表示同时转的次数最小的方向。
把一个串拼接两次,就可以得到环上的所有情况,顺时针直接得就好,逆时针,翻转后,找到最大,这样得到的是最后的位置(因为翻转了的缘故,可以模拟看看,翻转后,可以得到逆序的所有序列,但是顺序也会调换),用kmp找到最后完整出现的位置就好
#include <bits/stdc++.h>using namespace std;const int maxn = 4e5;int a[maxn],b[maxn],pos[maxn];int nxt[maxn],c[maxn];int getMax(int a[], int n){ int i, j, k; for(i = 0, j = 1; i<n&&j<n; ) { for(k = 0; k<n&&a[i+k]==a[j+k]; k++) ; if(k>=n) break; if(a[i+k]>a[j+k]) j += k+1; else i += k+1; if(i == j) j++; } return i;}int solve(string s,int a[],int n){ for(int i=0;i<n;i++) a[i]=s[i]-'a'; for(int i=n;i<2*n-1;i++) a[i]=a[i-n]; int po=getMax(a,n); return po;}void init_kmp(int m) { int i = 0, j = -1; nxt[0] = -1; while(i<m) { if(j == -1 || c[i] == c[j]) { ++i, ++j; nxt[i] = j; } else { j = nxt[j]; } }}int kmp(int n){ int cnt = 0; int i = 0, j = 0; while(i<2*n-1) { if(j == -1 || b[i] == c[j]) { i++, j++; } else { j = nxt[j]; } if(j == n) { pos[cnt++] = i; j = nxt[j]; } } return cnt;}int main(){ int t,n; string s; scanf("%d",&t); while(t--) { cin>>n>>s; int pos1=solve(s,a,n); int num=0,flag=-1; reverse(s.begin(),s.end()); int pos2=solve(s,b,n); int tmp=pos1,tmp2=pos2; while(num<n) { if(a[pos1]>b[pos2]) { flag=1; break; } else if(a[pos1]<b[pos2]) { flag=0; break; } else { pos1++; pos2++; num++; } } int cnt=0; for(int i=tmp2;i<tmp2+n;i++) c[cnt++]=b[i]; init_kmp(n); cnt=kmp(n); tmp2=pos[cnt-1]%n; if(num==n) { if(tmp+1<=n-tmp2) printf("%d 0\n",tmp+1 ); else printf("%d 1\n",n-tmp2 ); } else { if(flag) printf("%d 0\n",tmp+1 ); else printf("%d 1\n",n-tmp2 ); } }}
- hdu 5442 Favorite Donut(最大表示法+kmp)
- hdu 5442 Favorite Donut(最大表示法+kmp)
- HDU 5442 Favorite Donut 最大最小表示法+KMP
- hdu 5442 Favorite Donut (最大表示法+KMP)
- HDU 5442 Favorite Donut 最大表示法+KMP
- hdu 5442 Favorite Donut(最小表示法+kmp)
- hdu 5442 Favorite Donut(kmp+最小表示法)
- HDU 5442 Favorite Donut (最大表示法+KMP || 后缀数组)
- [HDU 5443]Favorite Donut[KMP][最大表示]
- HDU 5442 Favorite Donut(最大表示法)
- HDU 5442 Favorite Donut(字符串最大表示法)
- hdoj 5442 Favorite Donut 【KMP最大表示法 后缀数组】
- hdu5442 Favorite Donut(最大表示法+KMP)
- HDU5442 Favorite Donut(KMP+最大表示法)
- HDU 5442 Favorite Donut(最大表示法 + KMP 2015长春网络赛)
- hdu 5442 Favorite Donut 最小表示法+KMP
- hdu 5442 Favorite Donut 最小表示法+KMP 2015长春网络赛
- hdu 5442 F - Favorite Donut 后缀数组 / 字符串の最小表示法+kmp
- 404
- android studio环境配置视频教程
- 重构树 Fiolki
- 错排两大公式
- spring boot事物
- hdu 5442 Favorite Donut(最大表示法+kmp)
- 矩阵中的路径
- Android 使用iperf测试wifi吞吐量
- List的clone方法的应用
- Qt无边框窗口简单示例
- c语言操作mysql和数据库基本操作
- 推荐3个微信小程序地图接口Api
- 暑期集训总结
- 【Redis】windows环境下redis的安装与python的简单调用