链接:

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)的更多相关文章

  1. HDU - 3374:String Problem (最小表示法模板题)

    Give you a string with length N, you can generate N strings by left shifts. For example let consider ...

  2. hdu String Problem(最小表示法入门题)

    hdu 3374 String Problem 最小表示法 view code#include <iostream> #include <cstdio> #include &l ...

  3. HDU 3374 String Problem (KMP+最大最小表示)

    HDU 3374 String Problem (KMP+最大最小表示) String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  4. hdu 3374 String Problem(kmp+最小表示法)

    Problem Description Give you a string with length N, you can generate N strings by left shifts. For ...

  5. HDU - 3374 String Problem (kmp求循环节+最大最小表示法)

    做一个高产的菜鸡 传送门:HDU - 3374 题意:多组输入,给你一个字符串,求它最小和最大表示法出现的位置和次数. 题解:刚刚学会最大最小表示法,amazing.. 次数就是最小循环节循环的次数. ...

  6. HDU 3374 String Problem(KMP+最大/最小表示)

    String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  7. String Problem HDU - 3374(最大最小表示法+循环节)

    题意: 给出一个字符串,问这个字符串经过移动后的字典序最小的字符串的首字符位置和字典序最大的字符串的首字符的位置,和能出现多少次最小字典序的字符串和最大字典序的字符串 解析: 能出现多少次就是求整个字 ...

  8. HDU 3374 String Problem

    最大最小表示法与KMP求循环节 最大最小表示法 最大最小表示法与KMP求循环节的模板题, #include <iostream> #include <cstdio> #incl ...

  9. hdu 3374 String Problem(最小表示法+最大表示法+kmp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题意:给出一个字符串问这个字符串最小表示的最小位置在哪,还有有几个最小表示的串.最大表示的位置在 ...

随机推荐

  1. Jmeter之设置线程组运行次数/时间

    线程组的设置 线程组运行的次数=线程数*循环次数 Ramp-Up Period:表示启动时间 例如:线程数:10,循环次数:10,Ramp-Up Period:2 表示,这个线程组一共有100个线程( ...

  2. 8-Perl 哈希

    1.Perl 哈希哈希是 key/value 对的集合.Perl中哈希变量以百分号 (%) 标记开始.访问哈希元素格式:${key}.以下是一个简单的哈希实例:#!/usr/bin/perl%data ...

  3. asp.net 10 Cookie & Session

    Cookie 1.什么是Cookie 一小段文本,明文的数据,关于网站相关的文本字符串数据.一个客户端状态保持机制~ 存储在客户端的浏览器内存里面或者磁盘(如果不指定过期时间,那么存储在客户端浏览器内 ...

  4. ModbusTCP报文详解【二】

    [1]功能码05H [2]功能码06H [3]功能码0FH [4]功能码10H

  5. c#调用带用户名密码验证的wsdl

    之前记录过一篇添加带验证的webservice,但是公司的另一个项目是.net framework2.0的项目,没有服务引用,只能添加web引用. 现在记录和分享一下方法: 先添加web引用,选择ws ...

  6. nginx 配置反向代理和负载均衡

    Nginx的配置文件: nginx安装目录/conf/nginx.conf 重新加载配置文件 ./nginx -s reload 配置虚拟主机 一个server就是一台虚拟主机 server { li ...

  7. 1-10000以内的完数(js)

    //1-10000以内的完数 //完数:因子之和相加等于这个数 //例如:6的因子为1,2,3:1+2+3=6 // 6 // 28 // 496 // 8128 let sum = 0, i, j; ...

  8. vscode快捷操作

    Ctrl + `                     打开或关闭终端 Ctrl + Shift + n         打开或关闭新窗口 Ctrl + Shift + f 打开视图,显示编辑器左侧 ...

  9. Perl数据类型

    Perl 是一种弱类型语言,所以变量不需要指定类型,Perl 解释器会根据上下文自动选择匹配类型. Perl 有三个基本的数据类型:标量.数组.哈希.以下是这三种数据类型的说明: 标量 标量是Perl ...

  10. ETL 工具和 BI 工具

    ETL是数据仓库中的非常重要的一环,是承前启后的必要的一步.ETL负责将分布的.异构数据源中的数据如关系数据.平面数据文件等抽取到临时中间层后进行清洗.转换.集成,最后加载到数据仓库或数据集市中,成为 ...