KMP求循环节。
 
  循环节推导的证明相当的好,这题是很裸的套算法的题。
代码如下:
 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int N = ;
char buf[N];
int next[N]; void getNext(char *str) {
char *si = str;
int *ni = next;
int j = *next = -;
while (*si) {
while (j > - && *si != *(str + j)) j = *(next + j);
si++, ni++, j++;
// if (*si == *(str + j)) *ni = *(next + j);
// else *ni = j;
*ni = j;
}
} int minMaxExp(char *s, bool mini) {
int i = , j = , k = , t;
int len = strlen(s);
while (i < len && j < len && k < len) {
// cout << i << ' ' << j << ' ' << k << endl;
t = s[(i + k) % len] - s[(j + k) % len];
if (!t) k++;
else {
if (mini ^ (t > )) j += k + ;
else i += k + ;
if (i == j) j++;
k = ;
}
}
return min(i, j);
} int main() {
while (cin >> buf) {
getNext(buf);
// for (int i = 0, sz = strlen(buf); i < sz; i++) cout << next[i] + 1 << ' '; cout << endl;
int cycle = strlen(buf);
// cout << "got next" << endl;
cycle /= (cycle - next[cycle - ] - );
cout << minMaxExp(buf, true) + << ' ' << cycle << ' ' << minMaxExp(buf, false) + << ' ' << cycle << endl;
}
return ;
}
 
——written by Lyon

hdu 3374 String Problem (字符串最小最大表示 + KMP求循环节)的更多相关文章

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

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

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

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

  3. HDU 3374 String Problem

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

  4. HDU 3746 Cyclic Nacklace (KMP求循环节问题)

    <题目链接> 题目大意: 给你一个字符串,要求将字符串的全部字符最少循环2次需要添加的字符数. [>>>kmp next函数 kmp的周期问题]  #include &l ...

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

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

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

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

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题目大意:给出一个字符串,依次左移一个单位形成一堆字符串,求其字典序最小和最大的字符串需要左移多 ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题目大意:输出最大和最小的是从哪一位开始的,同时输出最小循环节的个数. 这里简单介绍对字符串最小 ...

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

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

随机推荐

  1. JZOJ 5455 拆网线

    Time Limits: 1000 ms Memory Limits: 65536 KB Detailed Limits Description 企鹅国的网吧们之间由网线互相连接,形成一棵树的结构.现 ...

  2. 洛谷 2055 [ZJOI2009]假期的宿舍——二分图匹配

    题目:https://www.luogu.org/problemnew/show/P2055 #include<iostream> #include<cstdio> #incl ...

  3. 安装 cx_Oracle

    1.下载 oracle client instant  和  sdk,  全部解压到 /opt/instantclient_11_2/ http://www.oracle.com/technetwor ...

  4. Watering Grass (贪心,最小覆盖)

    参考: https://blog.csdn.net/shuangde800/article/details/7828675 https://www.cnblogs.com/haoabcd2010/p/ ...

  5. HTML 实体字符

    有些字符,像(<)这类的,对HTML来说是有特殊意义的,所以这些字符是不允许在文本中使用的.要在HTML中显示(<)这个字符,我们就必须使用实体字符. 实体字符 有一些字符对HTML来讲是 ...

  6. NFS服务器搭建与配置

    原文:https://blog.csdn.net/qq_38265137/article/details/83146421 NFS服务简介什么是NFS?NFS就是Network File System ...

  7. ubuntu上安装notepadpp

    Notepad++是一套非常有特色的自由软件的纯文字编辑器(许可证:GPL).有完整的中文化接口及支持多国语言编写的功能(UTF8 技术).它的功能比 Windows 中的 Notepad(记事本)强 ...

  8. pl/sql进阶——例外处理

    在pl/sql的执行过程中发生异常时系统所作的处理称为一个例外情况(exception).通常例外情况的种类有三种: ①预定义的oracle例外情况,oracle预定义的例外情况大约有24个,对于这种 ...

  9. PHPCMS快速建站系列之类别调用及类别显示页面

    在需要调用类别的地方,比如列表页,首先写循环前面写上一句: <?php $TYPE = getcache('type_content','commons');?> 这句就是把类别缓存加载进 ...

  10. LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design

    字典树(Trie树相关) 208. Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith  ...