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 题意:给出一个字符串问这个字符串最小表示的最小位置在哪,还有有几个最小表示的串.最大表示的位置在 ...
随机推荐
- 【ActiveReports 大数据分析报告】2019国庆旅游出行趋势预测
今年国庆假期全国接待国内游客人数有望达到8亿人次! 随着2019国庆小长假的临近,不少游客已经开始着手规划假期出游路线.据权威机构发布的<2019国庆旅游趋势预测报告>显示,今年“十一黄金 ...
- Linux就该这么学——新手必须掌握的命令之系统状态检测命令组
ifconfig命令 用途 : 获取网卡配置与网络状态等信息 格式 : ifconfig[网络设备][参数] 其实主要查看的就是网卡名称,inet参数后面的IP地址,ether参数后面的网卡物理地址( ...
- django时区与时间差的问题
时区的正确配置方式: # 这里还可以配置成中文 一般用不到 LANGUAGE_CODE = 'en-us' # TIME_ZONE = 'UTC' TIME_ZONE = 'Asia/Shanghai ...
- 论文笔记-IGCV3:Interleaved Low-Rank Group Convolutions for Efficient Deep Neural Networks
论文笔记-IGCV3:Interleaved Low-Rank Group Convolutions for Efficient Deep Neural Networks 2018年07月11日 14 ...
- Lua的API函数
1. 基础库 我们在整个教程中使用了各种主题下的基本库. 下表提供了相关页面的链接,并列出了本Lua教程各部分所涵盖的功能. 编号 库/方法 作用 1 错误处理 包括错误处理函数,如断言, 错误,如L ...
- luogu题解P2502[HAOI2006]旅行--最小生成树变式
题目链接 https://www.luogu.org/problemnew/show/P2502 分析 一个很\(naive\)的做法是从\(s\)到\(t\)双向BFS这当然会TLE 这时我就有个想 ...
- Python算法题(一)——青蛙跳台阶
题目一(青蛙跳台阶): 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 分析: 假设只有一级台阶,则总共只有一种跳法: 假设有两级台阶,则总共有两种跳法: ...
- mark ubuntu 16.04 64bit + cpu only install mtcnn
大神代码链接 称之为MTCNN人脸检测算法,同时有大神已经GitHub上开源了其基于caffe的C++ API 的源代码,https://github.com/DaFuCoding/MTCNN_Caf ...
- Express bodyParser中间件使用方式
bodyParser中间件用来解析http请求体,是express默认使用的中间件之一. 1.这个模块提供以下解析器 (1) JSON body parser (2) Raw body parser ...
- 第十章、datetime模块
目录 第十章.datetime模块 一.datetime 模块 第十章.datetime模块 一.datetime 模块 import datetime#返回当前时间 print(datetime.d ...