HDOJ3374 String Problem 【KMP】+【最小表示法】
String Problem
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.
are multiple answers, choose the smallest one), and its times also.
abcder
aaaaaa
ababab
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】+【最小表示法】的更多相关文章
- hdu 3374 String Problem(kmp+最小表示法)
Problem Description Give you a string with length N, you can generate N strings by left shifts. For ...
- hdu3374 String Problem【最小表示法】【exKMP】
String Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 3374 String Problem(最小表示法+最大表示法+kmp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题意:给出一个字符串问这个字符串最小表示的最小位置在哪,还有有几个最小表示的串.最大表示的位置在 ...
- 【HDU3374】 String Problem (最小最大表示法+KMP)
String Problem Description Give you a string with length N, you can generate N strings by left shift ...
- hdu3374 String Problem KMP+最大最小表示法
Give you a string with length N, you can generate N strings by left shifts. For example let consider ...
- hdu5442(2015长春赛区网络赛1006)后缀数组+KMP /最小表示法?
题意:给定一个由小写字母组成的长度为 n 的字符串,首尾相连,可以从任意一个字符开始,顺时针或逆时针取这个串(长度为 n),求一个字典序最大的字符串的开始字符位置和顺时针或逆时针.如果有多个字典序最大 ...
- hdu3374 kmp+最小表示法
Give you a string with length N, you can generate N strings by left shifts. For example let consider ...
- hdu-3374(kmp+最小表示法)
题意:给你一个字符串,这个字符串我们可以把把他变成n个字符串按照以下规则:将当前字符串第一个放到字符串最后一位,字符串的下标依次向前推一位,比如:s[1] s[2 ]s[3] s[4]->s[2 ...
- bzoj2176 Strange string(字符串最小表示法)
Time Limit: 10 Sec Memory Limit: 259 MB 给定一个字符串S = {S1, S2, S3 … Sn}, 如果在串SS中, 子串T(|T| = n)为所有长度为n的 ...
随机推荐
- CSS3初步
一.CSS与CSS3的区别 非常简单,CSS代表"Casading Style Sheets",就是样式表,是一种替代并为网站添加样式的标记性语言.现在所使用的CSS基本是在199 ...
- Nginx学习之四-Nginx进程同步方式-自旋锁(spinlock)
自旋锁简介 Nginx框架使用了三种消息传递方式:共享内存.套接字.信号. Nginx主要使用了三种同步方式:原子操作.信号量.文件锁. 基于原子操作,nginx实现了一个自旋锁.自旋锁是一种非睡眠锁 ...
- CSS基础要点概况
1.CSS概述 1)css指层叠样式表 2)样式定义如何显示HTML元素 3)样式通常存储在样式表中 4)把样式添加到HTML4.0中,是为了解决内容与表现分离的问题 5)外部样式表可以极大提高工作效 ...
- Oozie — What Why and How
Oozie是什么? Oozie最初是Yahoo!为Hadoop开发的一个工作流调度器,一个工作流有多个Job组成.它允许用户提交由多个Job组成的工作流配置文件,这些Job既可以顺序执行,也可以并行执 ...
- 使用react-native做一个简单的应用-01项目介绍
学习react-native也有一个月的时间了.当学习了关于react-native的基础知识之后,打算自己去仿一个应用去练手.于是花了10天左右的时间,这个小应用的基本功能也实现的差不多了. 在展示 ...
- RelativeLayout与LinearLayout的区别
1.LinearLayout是线性布局控件,它包含的子控件将以横向或竖向的方式排列,按照相对位置来排列所有的widgets或者其他的 containers,超过边界时,某些控件将缺失或消失.因此一个垂 ...
- Spring学习之Ioc控制反转(1)
开始之前: 1. 本博文为原创,转载请注明出处 2. 作者非计算机科班出身,如有错误,请多指正 ---------------------------------------------------- ...
- Yii中常用路径(转)
调用YII框架中jquery:Yii::app()->clientScript->registerCoreScript('jquery'); framework/web/js/sou ...
- 深刻理解Oracle数据库的启动和关闭 .
Oracle数据库提供了几种不同的数据库启动和关闭方式,本文将详细介绍这些启动和关闭方式之间的区别以及它们各自不同的功能. 一.启动和关闭Oracle数据库 对于大多数Oracle DBA来说,启动和 ...
- qq电脑管家抽奖页面源代码
桌面qq电脑管家中个人中心的抽奖页面竟然是HTML写的,光大我HTML.!!! <!DOCTYPE html> <html> <head lang="en&qu ...