HDU-3374-String Problem(最小表示法, KMP)
链接:
https://vjudge.net/problem/HDU-3374
题意:
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.
思路:
最小表示法求出, 最大和最小字典序的串,
kmp求循环节.
但是数据好像不强,
abcabcabca和aabcabcabc, 两组数据答案循环的次数跑出来的不同, 但是却过了
不知道是不是题没读懂.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 1e6+10;
const int MOD = 1e4+7;
char ori[MAXN];
int Next[MAXN];
int GetMin(char *s)
{
//字符串的最小表示法
int len = strlen(s);
int i = 0, j = 1, k = 0;
//i为第一个字符串的开头, j为第二个字符串的开头, k为长度.
while (i < len && j < len && k < len)
{
int cmp = s[(i+k)%len]-s[(j+k)%len];
if (cmp == 0)
k++;
else
{
if (cmp > 0)
i += k + 1;
else
j += k + 1;
if (i == j)
j++;
k = 0;
}
}
return min(i, j);
}
int GetMax(char *s)
{
//字符串的最大表示法
int len = strlen(s);
int i = 0, j = 1, k = 0;
//i为第一个字符串的开头, j为第二个字符串的开头, k为长度.
while (i < len && j < len && k < len)
{
int cmp = s[(i+k)%len]-s[(j+k)%len];
if (cmp == 0)
k++;
else
{
if (cmp < 0)
i += k + 1;
else
j += k + 1;
if (i == j)
j++;
k = 0;
}
}
return min(i, j);
}
void GetNext(char *t)
{
int i = 0, k = -1;
int len = strlen(t);
Next[0] = -1;
while (i < len)
{
if (k == -1 || t[i] == t[k])
{
++i;
++k;
Next[i] = k;
}
else
k = Next[k];
}
}
int main()
{
while (~scanf("%s", ori))
{
int mmin = GetMin(ori);
int mmax = GetMax(ori);
int len = strlen(ori);
GetNext(ori);
int cyc = len/(len-Next[len]);
printf("%d %d %d %d\n", mmin+1, cyc, mmax+1, cyc);
}
return 0;
}
HDU-3374-String Problem(最小表示法, KMP)的更多相关文章
- HDU - 3374:String Problem (最小表示法模板题)
Give you a string with length N, you can generate N strings by left shifts. For example let consider ...
- hdu String Problem(最小表示法入门题)
hdu 3374 String Problem 最小表示法 view code#include <iostream> #include <cstdio> #include &l ...
- HDU 3374 String Problem (KMP+最大最小表示)
HDU 3374 String Problem (KMP+最大最小表示) String Problem Time Limit: 2000/1000 MS (Java/Others) Memory ...
- hdu 3374 String Problem(kmp+最小表示法)
Problem Description Give you a string with length N, you can generate N strings by left shifts. For ...
- HDU - 3374 String Problem (kmp求循环节+最大最小表示法)
做一个高产的菜鸡 传送门:HDU - 3374 题意:多组输入,给你一个字符串,求它最小和最大表示法出现的位置和次数. 题解:刚刚学会最大最小表示法,amazing.. 次数就是最小循环节循环的次数. ...
- HDU 3374 String Problem(KMP+最大/最小表示)
String Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- String Problem HDU - 3374(最大最小表示法+循环节)
题意: 给出一个字符串,问这个字符串经过移动后的字典序最小的字符串的首字符位置和字典序最大的字符串的首字符的位置,和能出现多少次最小字典序的字符串和最大字典序的字符串 解析: 能出现多少次就是求整个字 ...
- HDU 3374 String Problem
最大最小表示法与KMP求循环节 最大最小表示法 最大最小表示法与KMP求循环节的模板题, #include <iostream> #include <cstdio> #incl ...
- hdu 3374 String Problem(最小表示法+最大表示法+kmp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题意:给出一个字符串问这个字符串最小表示的最小位置在哪,还有有几个最小表示的串.最大表示的位置在 ...
随机推荐
- 【转载】Python第三方库资源
转自:https://weibo.com/ttarticle/p/show?id=2309404129469920071093 参考:https://github.com/jobbole/awesom ...
- 简单Kibana命令
1 查看健康状态 GET _cat/health?v epoch timestamp cluster status node.total node.data shards 1531290005 14: ...
- 运用加密技术保护Java源代码(转)
出处:运用加密技术保护Java源代码 为什么要加密? 对于传统的C或C++之类的语言来说,要在Web上保护源代码是很容易的,只要不发布它就可以.遗憾的是,Java程序的源代码很容易被别人偷看.只要有一 ...
- Jmeter之压测探索和结果分析
1.copy过来的,很有道理的一句话~ 最大并发数:取决于你的业务类型,数据量,处理时的资源需求等,具体多少,需要做一些性能测试来衡量 确定待测试的场景,设计脚本,不断增加并发数量. 2.CPU压不上 ...
- Python库的优雅安装及PyCharm虚拟环境配置
一.安装python库 安装python库有几种方式: 1. 使用pip命令行,如:pip install Pillow 2. 在pycharm中安装 3. 使用Anaconda批量安装常用模块 在使 ...
- List与Set区别
List: 元素有序放入,元素可重复 Set: 元素无序保存,元素不可重复(通过==判断,非基本类型判断的是引用地址),因为set是无序的,故只能通过迭代器循环.ps:说是无序,但是其实set中的元素 ...
- HBASE学习笔记(五)
一.HBase的RowKey设计原则 1.我们知道HBase是三维有序存储的,通过RowKey(行键),ColumnKey(Column family和qualifier)和TimeStamp(时间戳 ...
- javascript中 visibility和display区别在哪
差异: 1.占用的空间不同. 可见性占用域空间,而显示不占用. 可见性和显示可以隐藏页面,例如: 将元素显示属性设置为block将在该元素后换行. 将元素显示属性设置为inline将消除元素换行. 将 ...
- 1 sql server 利用多重赋值将一列的数据以逗号分隔,返回
declare @mav varchar(max) select @mav=coalesce(@mav+', '+d.Name,d.Name) from ( select Name from Huma ...
- Angular 开发环境搭建
Angular 是一款开源 JavaScript 框架,由Google 维护,用来协助单一页面应用程序运行的.它的目标是增强基于浏览器的应用,使开发和测试变得更加容易.目前最新的 Angular 版本 ...