Give you a string with length N, you can generate N strings by left shifts. For example let consider the string “SKYLONG”, we can generate seven strings:
String Rank 
SKYLONG 1
KYLONGS 2
YLONGSK 3
LONGSKY 4
ONGSKYL 5
NGSKYLO 6
GSKYLON 7
and lexicographically first of them is GSKYLON, lexicographically last is YLONGSK, both of them appear only once.
  Your task is easy, calculate the lexicographically fisrt string’s Rank (if there are multiple answers, choose the smallest one), its times, lexicographically last string’s Rank (if there are multiple answers, choose the smallest one), and its times also.

题意:给出一个字符串,求出它循环同构的字符串中字典序最小的一个串的开始位置,以及有多少串是同样字典序最小的,然后同样求出字典序最大的串的这两个值

先用最大/最小表示法求出字典序最大或最小的串,并在原串的倍增串中进行KMP匹配。

 #include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int maxn=1e6+;
char s[maxn<<],ss[maxn<<];
char tmp[maxn];
int p[maxn<<]; inline int max(int a,int b){return a>b?a:b;}
inline int min(int a,int b){return a<b?a:b;} int KMP(char s[],char t[]){
int i,j,ans=; //ans记录字符串出现次数
int n=strlen(s),m=strlen(t); //在题目中遇到过,其实strlen很慢,所以如果不先存起来可能有TLE的风险
p[]=p[]=; //初始化自匹配数组
for(i=;i<m;i++){ //自匹配
j=p[i];
while(j&&t[i]!=t[j])j=p[j];
p[i+]=t[i]==t[j]?j+:;
}
j=; //注意 j=0
for(i=;i<n-;i++){ //串匹配
while(j&&s[i]!=t[j])j=p[j];
if(s[i]==t[j])j++;
if(j==m){
ans++; //此处记录出现次数(模板串在待匹配串中可重叠),或改为直接break表示是否出现过
}
}
return ans;
} int MINR(char s[],int l){
for(int i=;i<l;++i)s[l+i]=s[i];
s[*l]=;
int i=,j=;
while(i<l&&j<l){
int k=;
while(s[i+k]==s[j+k]&&k<l)++k;
if(k==l)return min(i,j);
if(s[i+k]>s[j+k])i=max(i+k+,j+);
else j=max(j+k+,i+);
}
return min(i,j);
} int MAXR(char s[],int l){
for(int i=;i<l;++i)s[l+i]=s[i];
s[*l]=;
int i=,j=;
while(i<l&&j<l){
int k=;
while(s[i+k]==s[j+k]&&k<l)++k;
if(k==l)return min(i,j);
if(s[i+k]<s[j+k])i=max(i+k+,j+);
else j=max(j+k+,i+);
}
return min(i,j);
} int main(){
while(scanf("%s",s)!=EOF){
int l=strlen(s);
strcpy(ss,s);
int pos=MINR(ss,l);
for(int i=;i<l;++i)tmp[i]=ss[i+pos];
tmp[l]=;
printf("%d %d ",pos+,KMP(ss,tmp));
strcpy(ss,s);
pos=MAXR(ss,l);
for(int i=;i<l;++i)tmp[i]=ss[i+pos];
tmp[l]=;
printf("%d %d\n",pos+,KMP(ss,tmp));
}
return ;
}

hdu3374 String Problem KMP+最大最小表示法的更多相关文章

  1. O - String Problem KMP 字符串最小表示法

    Give you a string with length N, you can generate N strings by left shifts. For example let consider ...

  2. String Problem hdu 3374 最小表示法加KMP的next数组

    String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. HDU 3374 String Problem (KMP+最大最小表示)

    KMP,在有循环节的前提下: 循环节 t = len-next[len], 个数num = len/(len-next[len]);个人理解,如果有循环节,循环节长度必定小于等于len/2, 换句话说 ...

  4. HDU 3374 String Problem(KMP+最大(最小)表示)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题目大意:给出一个字符串,依次左移一个单位形成一堆字符串,求其字典序最小和最大的字符串需要左移多 ...

  5. HDU3374 String Problem —— 最小最大表示法 + 循环节

    题目链接:https://vjudge.net/problem/HDU-3374 String Problem Time Limit: 2000/1000 MS (Java/Others)    Me ...

  6. bzoj5130 字符串的周期(kmp,最小表示法)

    bzoj5130 字符串的周期(kmp,最小表示法) bzoj 题解时间 m很大,n很小. 周期很容易求,就是kmp之后n-fail[n]. 之后对于枚举所有的字符串用最小表示法,暴力搜索. 能过就完 ...

  7. hdu 3374 String Problem(kmp+最小表示法)

    Problem Description Give you a string with length N, you can generate N strings by left shifts. For ...

  8. hdu3374 String Problem【最小表示法】【exKMP】

    String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  9. hdu3374 String Problem

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题目: String Problem Time Limit: 2000/1000 MS (Java/ ...

随机推荐

  1. Linux电源管理-Linux regulator framework概述

    前言 1.  什么是regulator?      regulator翻译为"调节器",分为voltage regulator(电压调节器)和current(电流调节器).一般电源 ...

  2. int(a) 和 (int &) a 及 数据存储地址的探究

    做题做到一个很有意思的题 void main() { float a = 1; cout << boolalpha << ((int)a == (int &)a); f ...

  3. [HDU1890]RoboticSort

    Problem 每次找到最小值,然后把它和它前面的数翻转,然后找第二小数······ 然后输出这些数的下标. Solution 用splay维护,每次找到最小值,然后翻转前面区间. Notice 细节 ...

  4. DRF的分页

    DRF的分页   DRF的分页 为什么要使用分页 其实这个不说大家都知道,大家写项目的时候也是一定会用的, 我们数据库有几千万条数据,这些数据需要展示,我们不可能直接从数据库把数据全部读取出来, 这样 ...

  5. :装饰者模式--Beverage

    #ifndef __BEVERAGE_H__ #define __BEVERAGE_H__ #include <string> using namespace std; class Bev ...

  6. centos7中docker操作

    docker部署nginx 1. 下载nginx [root@localhost my.Shells]# docker images REPOSITORY TAG IMAGE ID CREATED S ...

  7. DevExpress WinForms使用教程:Ribbon性能

    [DevExpress WinForms v18.2下载] DevExpress XAF团队提供Ribbon新能改进,其中XAF Office Module的实际应用程序需要花费很长时间才能加载,导致 ...

  8. shelly - HYMN TO INTELLECTUAL BEAUTY

    HYMN TO INTELLECTUAL BEAUTY III No voice from some sublimer world hath ever ⁠To sage or poet these r ...

  9. L260

    Innovative UK technology that can deliver drugs deep into the brain to treat neurological diseases, ...

  10. 关于时间戳截取的隐藏bug

    之前写时间戳,要截取后六位 原写法: function timeStamp() { const date = new Date() const month = date.getMonth() + 1 ...