链接:

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. CentOS7之yum仓库配置

    操作系统版本:CentOS Linux release 7.2.1511 (Core)  Yum软件版本:yum-3.4.3-132.el7.centos.0.1.noarch  Yum主配置文件:/ ...

  2. Servlet 过滤器和异常处理

    Servlet 编写过滤器 Servlet 过滤器可以动态地拦截请求和响应,以变换或使用包含在请求或响应中的信息.可以将一个或多个 Servlet 过滤器附加到一个 Servlet 或一组 Servl ...

  3. CSP 通信网络(201709-4)

    问题描述 某国的军队由N个部门组成,为了提高安全性,部门之间建立了M条通路,每条通路只能单向传递信息,即一条从部门a到部门b的通路只能由a向b传递信息.信息可以通过中转的方式进行传递,即如果a能将信息 ...

  4. springboot2.0处理任何异常返回通用数据格式

    异常分为以下三种 自定义异常 可预知异常 不可预知异常 下面具体说明如何分类处理,从而保证无论触发什么异常均可返回理想的自定义数据格式 ResultCode /** * Created by mrt ...

  5. PDO简单的DB类封装

    <?php class DB{ private $dbs = ""; private $fields = "*"; private $tables = n ...

  6. centos服务器之间相互挂载(samba)

    前提:假设A服务器ip为:192.168.1.101 ,B服务器ip为:192.168.1.102现在要求把A服务器的/mnt/test 路径下的文件夹 共享到B服务器的/home/ceshi 下. ...

  7. Sharepoint 开启App 配置App

    如果没有Enable app,打开app store的时候出出现错误: Sorry, apps are turned off. If you know who runs the server, tel ...

  8. QT获取屏幕分表率(PC、安卓)

    QRect screenRect = QGuiApplication::primaryScreen()->geometry(); double devicePixelRatio = QGuiAp ...

  9. JS基础_if练习三

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  10. 帝国cms 通过栏目获取某个栏目的详情

    当是某个栏目的时候,获取另外一个栏目下第一篇文章的详情,否则获取当前栏目下第一篇文章的详情 <?php $classid = $GLOBALS['navclassid']; if($classi ...