【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)个循环同构串,要求输出字典序最小的同构串的下标,字典 ...
随机推荐
- hdu 1587 Flowers
Flowers Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- nofollow标签如何使用
“nofollow”的意思是不传递权重,向网站站长提供了一种方式,即告诉搜索引擎“不要追踪此网页上的链接”或“不要追踪此特定链接”. nofllow的形式 1.<meta name=" ...
- C#做的一个加密/解密的类
转自:http://www.16aspx.com/Article/3904 using System; using System.Security.Cryptography; using System ...
- Android开发之Handler
我们都知道应用程序开启后,安卓会开启一个主线程(UI线程),主线程管理UI控件,进行事件分发.那为什么会出现Handler呢? 例如你要是点击一个 Button ,Android会分发事件到Butto ...
- c语言学习之基础知识点介绍(十八):几个修饰关键字和内存分区
一.几个修饰关键字 全局变量: 全局变量跟函数一样也分为声明和实现.如果是全局变量,实现在它调用之后,那么需要在调用之前进行声明.注意:全局变量的声明只能写在函数外,写在函数就不是全局变量了而是局部变 ...
- JS中的replace方法以及与正则表达式的结合应用
replace方法的语法是:stringobj.replace(rgexp, replacetext) 其中stringobj是字符串(string),reexp可以是正则表达式对象(regexp)也 ...
- struts 模型驱动
情景: 有一个用来处理用户的UserAction 类, 该动作类实现了 ModelDriven 接口. 当用户触发UserAction 动作时, ModelDriven 拦截器将调用相关UserAct ...
- Java的云打印Lodop
打印某一个网页上的内容我们都经常遇到过,比如网上申请港澳通行证时需要填写申请表,然后把申请表给打印出来.像这样的打印技术是怎么实现?这种打印可以通过一种叫云打印的插件来做,按我的理解云打印的技 ...
- 02_Jquery_01_id选择器
首先建立建立一个JavaWeb项目工程,导入jquery-1.5.1.js. (一)选择器--打印文本内容 [index.jsp] <%@ page language="java&qu ...
- GDI画验证码
Random r = new Random(); string str = ""; for (int i = 0; i < 5; i++) { int a= r.Next(0 ...