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的 ...
随机推荐
- 在C++中打印出变量的方法
在C++中只能显示出字符串,而如果要想打印出其他类型的变量,则只能将其先转换为字符串类型. 例如:想打印出int型变量value的值 int value; 则需: char str[1];//定义一 ...
- 几个简单的css设置问题:div居中,ul li不换行 ,内容超出自动变省略号等
1 div在页面居中的问题 1)position值为relative时(相对定位),css设置属性margin:0 auto;(0 auto,表示上下边界为0,左右则根据宽度自适应相同值,即居中)即 ...
- 【转】android camera(二):摄像头工作原理、s5PV310 摄像头接口(CAMIF)
关键词:android camera CMM 模组 camera参数 CAMIF平台信息:内核:linux系统:android 平台:S5PV310(samsung exynos 4210) 作者 ...
- C 语言中实现数据与方法的封装
在 C 语言中可以用结构体代替类,用函数指针代替成员方法,实现数据成员与成员方法的封装,在客户端写出的程序与 C++ 类似,唯一的不同是 C 语言中调用函数指针成员时必须将本对象的地址传给函数,因为 ...
- ZOJ1372 POJ 1287 Networking 网络设计 Kruskal算法
题目链接:problemCode=1372">ZOJ1372 POJ 1287 Networking 网络设计 Networking Time Limit: 2 Seconds ...
- BestCoder Round #14 B 称号 Harry And Dig Machine 【TSP】
称号:Harry And Dig Machine 哈哈 最终涨边粉色了,不easy呀.顺便写一道题解吧 题意:给一个m*n的矩阵,然后当中最多由10个有值,求总左上角把全部的值都拿上回到左上角的最小 ...
- 3第一周课后练习·阅读计划(3)-使用函数来访问私有数据成员
/* * Copyright (c) 2015, 计算机科学学院,烟台大学 * All rights reserved. * 文件名:test.cpp * 作 靠:刘畅 * 完成日期:2015年 3 ...
- android 根据网络来获取外网ip地址及国家,地区的接口
新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js 新浪多地域测试方法:http://int.dpool. ...
- 网页被Chrome识别成英语,区域,语言,网站
修改成这个后解决 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" ...
- oracle函数trunc的使用
1.日期比较时精确到日,可以使用 TRUNC(sysdate,'dd')函数.函数支持格式有:yyyy MM dd hh Mi可以用 select TRUNC(sysdate,'yyyy') ...