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. tocat 域名绑定

    修改tomcat/conf/server.xml配置文件,新增部分如下 <Service name="Catalina1"> <Connector port=&q ...

  2. Android知识补充(Android学习笔记)

    Android知识补充 ●国际化 所谓的国际化,就是指软件在开发时就应该具备支持多种语言和地区的功能,也就是说开发的软件能同时应对不同国家和地区的用户访问,并针对不同国家和地区的用户,提供相应的.符合 ...

  3. Cracking The Coding Interview 9.1

    //原文: // // You are given two sorted arrays, A and B, and A has a large enough buffer at the end to ...

  4. Java中泛型Class<T>、T与Class<?>

    一.区别 单独的T 代表一个类型 ,而 Class<T>代表这个类型所对应的类, Class<?>表示类型不确定的类 E - Element (在集合中使用,因为集合中存放的是 ...

  5. dialog销毁不干净与弹出多个dialog问题

    1.关闭dialog的时候不销毁.重新打开然后影响页面的效果与样式. 原因: dialog的close()只是将html片段隐藏,并没有销毁移除. 解决方式: 打开dialog的时候在写onClose ...

  6. SharePoint REST API - 列表和列表项

    博客地址:http://blog.csdn.net/FoxDave 本篇主要讲述如何用SharePoint REST操作列表和列表项.阅读本篇时请先了解前面讲述的REST介绍和基本操作. 废话不多 ...

  7. Codeforces Round #485 (Div. 2) C题求三元组(思维)

    C. Three displays time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  8. Word2Vec实现原理(Hierarchical Softmax)

    由于word2vec有两种改进方法,一种是基于Hierarchical Softmax的,另一种是基于Negative Sampling的.本文关注于基于Hierarchical Softmax的改进 ...

  9. golang fmt占位符

    golang fmt格式"占位符" golang 的fmt 包实现了格式化I/O函数,类似于C的 printf 和 scanf. 定义示例类型和变量 type Human stru ...

  10. 适用于WIFI Hacking的无线网卡推荐

    相信很多初次使用Kali Linux来进行无线渗透的小伙伴都曾遇到过一个非常头疼的问题,就是不知道如何选购一款合适的无线网卡.因为Kali Linux并不是所有的网卡都支持,一旦选错了网卡不仅会给我们 ...