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. 用大白菜U盘安装:[3]Ghost版Win7系统

    Ghost版Win7系统安装步骤: 1,先下载Ghost Win7系统到硬盘中,然后在U盘或其它硬盘根目录中新建一个GHO文件夹,注意:决不能把文件夹建在C盘(系统盘)中,然后用UltraISO或者W ...

  2. commons-logging 与log4j的关系

    参考:http://zachary-guo.iteye.com/blog/361177

  3. 【原创】连接数据库MySQL,读取、显示、修改数据

    /* Time: 2017.01.02 —— 2017.01.04 * Author: WJ * Function:连接数据库,从数据库中读取图片并显示(已成功) */ [参考链接] MySQL存入图 ...

  4. Jdbc连接Oracle12C集群环境

    jdbc.url=jdbc:Oracle:thin:@(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 192.31.0. ...

  5. 十七. Python基础(17)--正则表达式

    十七. Python基础(17)--正则表达式 1 ● 正则表达式 定义: Regular expressions are sets of symbols that you can use to cr ...

  6. [Leetcode221]最大面积 Maximal Square

    [题目] Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's a ...

  7. 重载方法写delete请求

    #encoding=utf-8#__author__="Lanyangyang" import unittestimport requestsimport json # This ...

  8. [PyImageSearch] Ubuntu16.04 使用OpenCV和python识别信用卡 OCR

    在今天的博文中,我将演示如何使用模板匹配作为OCR的一种形式来帮助我们创建一个自动识别信用卡并从图像中提取相关信用卡数位的解决方案. 今天的博文分为三部分. 在第一部分中,我们将讨论OCR-A字体,这 ...

  9. 通过泛型获得继承类的类原型getGenericSuperclass

    首先贴上代码 package com; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; /** * ...

  10. JavaScript作用域及作用域链详解、声明提升

    相信大家在入门JavaScript这门语言时对作用域.作用域链.变量声明提升这些概念肯定会稀里糊涂,下面就来说说这几个 Javascript 作用域 在 Javascript 中,只有局部作用域和全局 ...