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 题意:给出一个字符串问这个字符串最小表示的最小位置在哪,还有有几个最小表示的串.最大表示的位置在 ...
随机推荐
- (4.35)sql server清理过期文件【转】
在SQL Server中, 一般是用维护计划实现删除过期文件.不过直接用脚本也是可以的,而且更灵活. 下面介绍三种方法, 新建一个作业, 在作业的步骤里加上相关的脚本就可以了. --1. xp_del ...
- spring boot 项目开发常用目录结构
在spring boot开发中一些常用的目录划分 转载自https://blog.csdn.net/Auntvt/article/details/80381756: 一.代码层结构 根目录:net.c ...
- ApplicationListener原理分析
在 Nacos配置服务原理 文中结束时提到过通过发布 ApplicationListener 刷新事件完成 Context 中属性值的更新.那么本章我们一起分析 ApplicationListener ...
- 虚拟机(Vmware)安装ubuntu18.04和配置调整(三)
三.ubuntu安装软件 1.安装常用软件 python程序员: $ sudo apt install ipython $ sudo apt install ipython3 $ sudo a ...
- 购物车以php原生cookie实现
index.php //入口文件 <?php /** * @name index.php * @decs * @author 老猫 <18368091722@163.com> * U ...
- MySql 枚举和集合 详解
枚举与集合 枚举类型,enum 每个枚举值均有一个索引值: 在列说明中列表值所允许的成员值被从 1 开始编号. 一般来说就是单选,在定义枚举的时候列出所有的可能性: 代码如下 1. create ta ...
- mysql 8.x 集群出现:Last_IO_Error: error connecting to master 'repl@xxx:3306' - retry-time: 60 retries: 1
网上的经验:网络不同,账号密码不对,密码太长,密码由 # 字符:检查MASTER_HOST,MASTER_USER,MASTER_PASSWORD(不知道 MASTER_LOG_FILE 有没有影响) ...
- 进阶Java编程(13)反射与Annotation
1,反射取得Annotation信息 从JDK1.5之后Java提供了Annotation技术支持,这种技术为项目的编写带来了新的模型,而后经过了十年的发展,Annotation的技术得到了非常广泛的 ...
- JavaScript例子1-给网页中所有<p>元素添加onclick事件
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- Oracle连接字符串总结(转)
Oracle XE 标准连接 Oracle XE(或者"Oracle Database 10g Express Edition")是一个简单免费发布的版本. 以下是语法格式: Dr ...