hdu3374 kmp+最小表示法
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.OutputOutput 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
题意:找出一个字符串的循环列中最小和最大的那个字符串,并求出格数
题解:个数就是最小循环数,用kmp的next数组即可,然后用字符串的最小表示法求,最大的同理wa点:刚开始想直接暴力结果mle,莫名其妙,我以为应该是tle,结果改用kmp了之后,直接暴力求最小还是tle, 最后实在没办法看的最小表示法,还有一点就是自己脑残,写成了ans=(slen%ans ? ans:slen/ans)不能整除明明是1啊!!
不过还是没搞懂为啥不能直接循环求最小,时间复杂度明明是一样的,最小表示法时间复杂度也是O(n)啊
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 10007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1 using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=(<<)-,inf=0x3f3f3f3f; int Next[N],slen;
string str; void getnext()
{
int k=-;
Next[]=-;
for(int i=;i<slen;i++)
{
while(k>-&&str[k+]!=str[i])k=Next[k];
if(str[k+]==str[i])k++;
Next[i]=k;
}
}
int solve(bool flag)
{
int i=,j=,k=;
while(i<slen&&j<slen&&k<slen){
int t=str[(i+k)%slen]-str[(j+k)%slen];
if(!t)k++;
else
{
if(flag)t> ? i=i+k+:j=j+k+;
else t< ? i=i+k+:j=j+k+;
if(i==j)j++;
k=;
}
}
return min(i,j);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
// cout<<setiosflags(ios::fixed)<<setprecision(2);
while(cin>>str){
slen=str.size();
getnext();
int ans=slen-Next[slen-]-;
ans=(slen%ans ? :slen/ans);
cout<<solve()+<<" "<<ans<<" "<<solve()+<<" "<<ans<<endl;
}
return ;
}
hdu3374 kmp+最小表示法的更多相关文章
- hdu-3374(kmp+最小表示法)
题意:给你一个字符串,这个字符串我们可以把把他变成n个字符串按照以下规则:将当前字符串第一个放到字符串最后一位,字符串的下标依次向前推一位,比如:s[1] s[2 ]s[3] s[4]->s[2 ...
- hdu5442(2015长春赛区网络赛1006)后缀数组+KMP /最小表示法?
题意:给定一个由小写字母组成的长度为 n 的字符串,首尾相连,可以从任意一个字符开始,顺时针或逆时针取这个串(长度为 n),求一个字典序最大的字符串的开始字符位置和顺时针或逆时针.如果有多个字典序最大 ...
- hdu 3374 String Problem(kmp+最小表示法)
Problem Description Give you a string with length N, you can generate N strings by left shifts. For ...
- hdu3374 最大最小表示法 +kmp
#include <iostream> #include <algorithm> #include <string.h> #include <cstdio&g ...
- hdu3374最小表示法+KMP
题意: 给你一个最长100W的串,然后让你找到最小同构子串,还有最大同构子串的下标,最小同构子串就是把字符串连接成一个环,然后选择一个地方断开,得到的一个ASCII最小的子串(求最大同理) ...
- 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(最小表示法, KMP)
链接: https://vjudge.net/problem/HDU-3374 题意: Give you a string with length N, you can generate N stri ...
- 【KMP】【最小表示法】NCPC 2014 H clock pictures
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个 ...
- Manacher模板,kmp,扩展kmp,最小表示法模板
*N]; //储存临时串 *N];//中间记录 int Manacher(char tmp[]) { int len=strlen(tmp); ; ;i<len;i++) { s[cnt++]= ...
随机推荐
- js valueOf()函数用于返回指定对象的原始值
valueOf()函数用于返回指定对象的原始值. 该方法属于Object对象,由于所有的对象都"继承"了Object的对象实例,因此几乎所有的实例对象都可以使用该方法. 对象 返回 ...
- click 在网页测试手机模式下无效,不能执行。调成非手机模式即可
click 在网页测试手机模式下无效,不能执行. 调成非手机模式即可
- [转载] iframe嵌入网页的用法
iframe并不是很常用的,在标准的网页中非常少用.但是有朋友经常问到,下面我简单地介绍一下它的用法,你只要熟练掌握这些参数足矣. <iframe>也应该是框架的一种形式,它与<fr ...
- zookeeper与卡夫卡集群搭建
首先这片博客没有任何理论性的东西,只是详细说明kafka与zookeeper集群的搭建过程,需要三台linux服务器. java环境变量设置 zookeeper集群搭建 kafka集群搭建 java环 ...
- c++ 11开始语言本身和标准库支持并发编程
c++ 11开始语言本身和标准库支持并发编程,意味着真正要到编译器从语言和标准库层面开始稳定,估计得到17标准出来.14稳定之后的事情了,根据历史经验,新特性的引入到稳定被广泛采用至少要一个大版本的跨 ...
- VIM 使用心得
序 到百度外卖任职以后,发现在我们部门无论 mac 还是 windows,程序员们清一色地都在使用 VIM 来编辑代码,期间穿插着各种插件.快捷键.眼花缭乱的命令.我在大学时只会极少的 VIM 命令, ...
- Java并发之FairSync和NonfairSync
Java并发中的fairSync和NonfairSync主要区别为: 如果当前线程不是锁的占有者,则NonfairSync并不判断是否有等待队列,直接使用compareAndSwap去进行锁的占用; ...
- 02: shell中的if、case、for等语句
目录: 1.1 shell中常用运算符 1.2 使用if条件语句 1.3 shell 中的for循环 1.4 shell中的while循环语句 1.5 使用case分支语句 1.1 shell中常用运 ...
- Windows server利用批处理脚本判断端口, 启动tomcat
win server服务器上面的tomcat老是不定时挂掉, 于是利用定时操作脚本判断tomcat80端口是否在运行, 如果运行则放过, 如果down掉就启动tomcat,解决tomcat不定时挂掉导 ...
- 一款简单实用的jQuery图片画廊插件
图片画廊 今天分享一个自己实现的jQuery 图片画廊插件. 看一下效果图: 点击图片时: 在线演示地址:http://www.jr93.top/photoGallery/photoGallery.h ...