【HDU3374】 String Problem (最小最大表示法+KMP)
String ProblemDescription
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
abababSample Output
1 1 6 1
1 6 1 6
1 3 2 3
用最大最小表示法,KMP求个数。
注意特殊的时候在KMP中加入长度特判。
最小最大表示法是求出循环子串得到最小最大表示。
表示我之前并没有遇到过,貌似还有些神奇的应用?不知道、、
但是打起来还是挺简单的,也很容易证明。
int get_st(bool p)
{
int i=1,j=2,k=0;
while(i<=l&&j<=l&&k<=l)
{
if(s[i+k]==s[j+k]) k++;
else if((s[i+k]>s[j+k])==(!p))
{
i+=k+1;
k=0;
}
else
{
j+=k+1;
k=0;
}
if(i==j) j++;
}
return mymin(i,j);
}
这个我把最小表示和最大表示合在一起了。p=0是最小,p=1时最大。
证明的话...连续匹配过程中一旦发现字符有小于其他字符的,那么他一定不是最大表示,且他前面连续的一段也不是。
本题代码如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 1000010 char s[*Maxn];
int nt[Maxn*],l; int mymin(int x,int y) {return x<y?x:y;} int get_st(bool p)
{
int i=,j=,k=;
while(i<=l&&j<=l&&k<=l)
{
if(s[i+k]==s[j+k]) k++;
else if((s[i+k]>s[j+k])==(!p))
{
i+=k+;
k=;
}
else
{
j+=k+;
k=;
}
if(i==j) j++;
}
return mymin(i,j);
} void get_nt(int x)
{
nt[x]=x-;int p=x-;
for(int i=x+;i<=x+l-;i++)
{
while(s[i]!=s[p+]&&p>=x) p=nt[p];
if(s[i]==s[p+]) p++;
nt[i]=p;
}
} int ffind(int x)
{
int ans=;int p=x-;
for(int i=;i<=*l;i++)
{
while((s[i]!=s[p+]&&p>=x)||p-x+>=l) p=nt[p];
if(s[i]==s[p+]&&p-x+<l) p++;
if(i>l&&p-x+>=l) ans++;
}
return ans;
} int main()
{
while(scanf("%s",s+)!=EOF)
{
l=strlen(s+);
for(int i=;i<=l;i++) s[i+l]=s[i];
int st=get_st();
get_nt(st);
printf("%d %d ",st,ffind(st)); st=get_st();
get_nt(st);
printf("%d %d\n",st,ffind(st));
}
return ;
}
HDU3374
2016-08-17 15:59:59
【HDU3374】 String Problem (最小最大表示法+KMP)的更多相关文章
- HDU3374 String Problem —— 最小最大表示法 + 循环节
题目链接:https://vjudge.net/problem/HDU-3374 String Problem Time Limit: 2000/1000 MS (Java/Others) Me ...
- kuangbin专题十六 KMP&&扩展KMP HDU3347 String Problem(最小最大表示法+kmp)
Give you a string with length N, you can generate N strings by left shifts. For example let consider ...
- hdu3374 String Problem 最小最大表示法 最小循环节出现次数
#include <iostream> #include <cstring> #include <cstdio> using namespace std; int ...
- hdu String Problem(最小表示法入门题)
hdu 3374 String Problem 最小表示法 view code#include <iostream> #include <cstdio> #include &l ...
- hdu3374 String Problem【最小表示法】【exKMP】
String Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu3374 String Problem
地址:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题目: String Problem Time Limit: 2000/1000 MS (Java/ ...
- hdu3374 String Problem KMP+最大最小表示法
Give you a string with length N, you can generate N strings by left shifts. For example let consider ...
- HDU - 3374:String Problem (最小表示法模板题)
Give you a string with length N, you can generate N strings by left shifts. For example let consider ...
- HDU 3374 最小/大表示法+KMP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题意:给定一个串s,该串有strlen(s)个循环同构串,要求输出字典序最小的同构串的下标,字典 ...
随机推荐
- 学点css基础
中午时间学点css,附带http://www.w3cschool.cc/css/css-tutorial.html这个链接! 中午的时间学了这些东西!如下图: 附带代码: <!DOCTYPE h ...
- 从入行到现在(.net)
每次写东西都不知道怎么去开头.因为一想到要写东西.脑子里面浮现出来的开头就太多. 进园子很久从开始只是关注别人讲的技术,别人讲的基础,到现在更多的去看大家的随笔和新闻.这两年我从零基础的外行人逐渐进入 ...
- Meta http-equiv属性
http-equiv顾名思义,相当于http的文件头作用,它可以向浏览器传回一些有用的信息,以帮助正确和精确地显示网页内容,与之对应的属性值为content,content中的内容其实就是各个参数的变 ...
- MVC 避免黄页
可以使用HandleErrorAttribute 有两种方式可以使用它,在类或者方法的头上加 [HandleError] 这种直接在类或者方法上加[HandlerError]手动添加 另外一种方式是使 ...
- 利用Highcharts制作web图表学习(一)
前言:最近项目中需要对数据进行汇总和分析得出柱状图或曲线图这样散装点图,经过学习和测试发现Highchart不错,如果大家项目中需要的话,不妨看看有的例子摘自官网 一.先说说HighCharts的 ...
- html-----008
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Javascript 数组自定义排序,并获取排序后的保存原索引的同序数组(堆排序实现)
比如数组A: [ 0: 5, 1: 2, 2: 4, 3: 3, 4: 1 ] 排序后的结果为:[1, 2, 3, 4, 5],但是有时候会有需求想要保留排序前的位置到一个同位数组里,如前例则为:[4 ...
- redis基本数据类型【2】-Hash类型
一.概述 1.散列是一种典型的字典结构,filed和value的映射,但value只能存储字符串,不支持其他类型 2.一个散列类型最多包含 2^32 -1个字段 3.散列适合存储对象:使用对象和ID构 ...
- zoj 1649 Rescue (BFS)(转载)
又是类似骑士拯救公主,不过这个是朋友拯救天使的故事... 不同的是,天使有多个朋友,而骑士一般单枪匹马比较帅~ 求到达天使的最短时间,杀死一个护卫1 units time , 走一个格子 1 unit ...
- IOS 学习笔记 2015-03-20 O之 nil,Nil,NULL,NSNull
1.oc最好 用nil [ nil 任意方法],不会崩溃 nil 是一个对象值.NULL是一个通用指针(泛型指针). 2. NSNULL,NULL和nil在本质上应该是一样的,NULL和nil其 ...