String Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1512    Accepted Submission(s): 668
Problem Description
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.
 
Input
  Each line contains one line the string S with length N (N <= 1000000) formed by lower case letters.
 
Output
Output four integers separated by one space, lexicographically fisrt string’s Rank (if there are multiple answers, choose the smallest one), the string’s times in the N generated strings, lexicographically last string’s Rank (if there
are multiple answers, choose the smallest one), and its times also.
 
Sample Input
abcder
aaaaaa
ababab
 
Sample Output
1 1 6 1
1 6 1 6
1 3 2 3

这题之前做过一次。今天做的时候还是把最小表示法给搞忘了,用普通枚举求最小字典序,果断超时。

题意:给定一个串。原始串序号为1,每向左移动一次序号加一,求最小字典序的序号和最大字典序的序号以及循环次数。

题解:求循环次数仅仅须要找到循环节点,难点是找最小(大)字典序的序号,要用到最小表示法,这个就当成模板来用吧。最小表示法难理解的地方在s[i+k]>s[j+k]时i须要+=k+1,这是由于s[i...i+k-1]和s[j...j+k-1]相等。说明不论什么以s[i...i+k]開始的字符串都要大于以s[j]開始的字符串。

#include <stdio.h>
#define maxn 1000002 char str[maxn];
int next[maxn], len, cir, minPos, maxPos; void getNext()
{
int i = 0, j = -1;
next[0] = -1;
while(str[i]){
if(j == -1 || str[i] == str[j]){
++i; ++j;
next[i] = j; //mode 1
}else j = next[j];
}
len = i;
} void findMinAndMaxPos()
{
minPos = maxPos = 0;
int k = 0, pos = 1, t;
while(minPos < len && k < len && pos < len){
t = str[(minPos + k) % len] - str[(pos + k) % len];
if(t == 0){ ++k; continue; }
else if(t < 0) pos += k + 1;
else minPos += k + 1;
k = 0;
if(minPos == pos) ++pos;
}
if(minPos > pos) minPos = pos;
++minPos; k = 0; pos = 1;
while(maxPos < len && k < len && pos < len){
t = str[(maxPos + k) % len] - str[(pos + k) % len];
if(t == 0){ ++k; continue; }
else if(t > 0) pos += k + 1;
else maxPos += k + 1;
k = 0;
if(maxPos == pos) ++pos;
}
if(maxPos > pos) maxPos = pos;
++maxPos;
} int main()
{
//freopen("stdin.txt", "r", stdin);
while(scanf("%s", str) != EOF){
getNext();
findMinAndMaxPos(); cir = 1;
if(len % (len - next[len]) == 0)
cir = len / (len - next[len]);
printf("%d %d %d %d\n", minPos, cir, maxPos, cir);
}
return 0;
}

HDOJ3374 String Problem 【KMP】+【最小表示法】的更多相关文章

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

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

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

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

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题意:给出一个字符串问这个字符串最小表示的最小位置在哪,还有有几个最小表示的串.最大表示的位置在 ...

  4. 【HDU3374】 String Problem (最小最大表示法+KMP)

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

  5. hdu3374 String Problem KMP+最大最小表示法

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

  6. hdu5442(2015长春赛区网络赛1006)后缀数组+KMP /最小表示法?

    题意:给定一个由小写字母组成的长度为 n 的字符串,首尾相连,可以从任意一个字符开始,顺时针或逆时针取这个串(长度为 n),求一个字典序最大的字符串的开始字符位置和顺时针或逆时针.如果有多个字典序最大 ...

  7. hdu3374 kmp+最小表示法

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

  8. hdu-3374(kmp+最小表示法)

    题意:给你一个字符串,这个字符串我们可以把把他变成n个字符串按照以下规则:将当前字符串第一个放到字符串最后一位,字符串的下标依次向前推一位,比如:s[1] s[2 ]s[3] s[4]->s[2 ...

  9. bzoj2176 Strange string(字符串最小表示法)

    Time Limit: 10 Sec  Memory Limit: 259 MB 给定一个字符串S = {S1, S2, S3 … Sn}, 如果在串SS中, 子串T(|T| = n)为所有长度为n的 ...

随机推荐

  1. HBase--DependentColumnFilter(参考例过滤器 )详解

    DependentColumnFilter是一种允许用户指定一个参考列或引用列来过滤其他列的过滤器,过滤的原则是基于参考列的时间戳来进行筛选 . 官方说明: 大意:此过滤器提供两个参数--列族和列限定 ...

  2. vue+webpack项目实战

    概述 -- 项目中会用到的插件 vue-router vue-resource 打包工具 webpack 依赖环境 node.js start 安装vue开发的模板 # 全局安装 vue-cli $ ...

  3. C# Socket学习笔记一

    小记:刚接触网络编程觉得网络是个神奇的东西,所以对它就很有兴趣,想了解下网络是如何进行进行数据传输的,那么开始第一天的学习吧!ReadyGo!!! 首先我们要了解一下几点内容: 1.网络中进程之间如何 ...

  4. Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent

    修改pom.xml文件,添加以下属性 <project> ... <properties> <project.build.sourceEncoding>UTF-8& ...

  5. margin四个属性的顺序

    margin-top ,margin-right ,margin-bottom ,margin-left .方向为 上右下左,顺时针方向, 值可以是: 百分比(基于父对象总高度或宽度的百分比) 长度值 ...

  6. React-Native post和get请求

    post: fetchData (title) { fetch(REQUEST_URL, { method: 'POST', headers: { 'Accept': 'application/jso ...

  7. html基础之 表单提交方法

    最普通最常用最一般的方法就是用submit type..看代码: <form name=”form” method=”post” action=”#"> <input ty ...

  8. HDU 5724 - Chess

    题意:    一个n行20列的棋盘. 每一行有若干个棋子.     两人轮流操作, 每人每次可以将一个棋子向右移动一个位置, 如果它右边有一个棋子, 就跳过这个棋子, 如果有若干个棋子, 就将这若干个 ...

  9. spring注解注入:<context:component-scan>使用说明

    spring从2.5版本开始支持注解注入,注解注入可以省去很多的xml配置工作.由于注解是写入java代码中的,所以注解注入会失去一定的灵活性,我们要根据需要来选择是否启用注解注入. 在XML中配置了 ...

  10. Github android客户端源代码分析之一:环境搭建

    1.下载相应的包及项目,参考https://github.com/github/android/wiki/Building-From-Eclipse. 2.若需查看某些包的源文件或者javadoc,则 ...