当前位置: 首页 > news >正文

KMP + 求最小循环节 --- HDU 1358 Period

Period 

Problem's Link:  http://acm.hdu.edu.cn/showproblem.php?pid=1358


 

Mean: 

给你一个字符串,让你从第二个字符开始判断当前长度的字符串是否是重复串,如果是,输出当前位置,并输出重复串的周期。

analyse:

还是next数组的运用,详见上一篇博客。

Time complexity: O(N)

 

Source code: 

/*
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-07-28-07.12
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define  LL long long
#define  ULL unsigned long long
using namespace std;
const int MAXN = 1000010;
int n;
char s [ MAXN ];
int Next [ MAXN ];
void getNext()
{
      Next [ 0 ] = 0;
      for( int i = 1 , k = 0; i <n; ++ i)
      {
            while(s [ i ] !=s [ k ] && k) k = Next [ k - 1 ];
            if(s [ i ] ==s [ k ]) ++ k;
            Next [ i ] = k;
      }
}
int main()
{
      ios_base :: sync_with_stdio( false);
      cin . tie( 0);
      int cas = 1;
      while( ~ scanf( "%d" , &n) &&n)
      {
            scanf( "%s" ,s);
            getNext();
            printf( "Test case #%d \n " , cas ++);
            for( int i = 1; i <n; ++ i)
            {
                  int now_cycle =( i + 1) - Next [ i ];
                  if(( now_cycle != i + 1) && ( i + 1) % now_cycle == 0)
                  {
                        printf( "%d %d \n " , i + 1 ,( i + 1) / now_cycle);
                  }
            }
            puts( "");
      }
      return 0;
}
/*

*/

 

相关文章:

  • HDU-3605 Escape(状态压缩+最大流求多重匹配、改版匈牙利算法)
  • multiset的使用以及集合的运算
  • 为学IOS,进击中......
  • 2014-2015 ACM-ICPC, NEERC, Northern Subregional Contest I-Instruction(模拟)
  • jquery常用技巧及常用方法列表集合
  • Codeforces Round #439 (Div. 2) C.The Intriguing Obsession(组合数、记忆化搜索)
  • (第一天)包装对象、作用域、创建对象
  • UOJ-79 一般图的最大匹配(带花树模板求解)
  • POJ-2823 POJ-3250 (单调队列 单调栈)
  • 关于量价十八则的口诀
  • HDU-3478 Catch(二分图染色+并查集)
  • RSA密钥的生成与配置
  • POJ 2536 Gopher II
  • HDU-1043 Eight(经典八数码问题, A*+康拓+曼哈顿距离+逆序数判断可解性、双向搜索)
  • codeforces-510E Fox And Dinner(带限制的二分图多重匹配+奇偶建图+打印路径)
  • 5分钟即可掌握的前端高效利器:JavaScript 策略模式
  • Angular 4.x 动态创建组件
  • django开发-定时任务的使用
  • javascript数组去重/查找/插入/删除
  • JS笔记四:作用域、变量(函数)提升
  • js中的正则表达式入门
  • Linux后台研发超实用命令总结
  • Meteor的表单提交:Form
  • October CMS - 快速入门 9 Images And Galleries
  • Promise初体验
  • vuex 笔记整理
  • Work@Alibaba 阿里巴巴的企业应用构建之路
  • 浏览器缓存机制分析
  • 前端技术周刊 2019-02-11 Serverless
  • 前端之React实战:创建跨平台的项目架构
  • 使用parted解决大于2T的磁盘分区
  • 想使用 MongoDB ,你应该了解这8个方面!
  • 这几个编码小技巧将令你 PHP 代码更加简洁
  • (react踩过的坑)Antd Select(设置了labelInValue)在FormItem中initialValue的问题
  • (Redis使用系列) Springboot 整合Redisson 实现分布式锁 七
  • (附源码)spring boot儿童教育管理系统 毕业设计 281442
  • (力扣记录)235. 二叉搜索树的最近公共祖先
  • (七)c52学习之旅-中断
  • (数位dp) 算法竞赛入门到进阶 书本题集
  • (一)插入排序
  • (译)2019年前端性能优化清单 — 下篇
  • ****** 二 ******、软设笔记【数据结构】-KMP算法、树、二叉树
  • ./configure、make、make install 命令
  • .NET CLR基本术语
  • .net core 6 集成和使用 mongodb
  • .NET Core日志内容详解,详解不同日志级别的区别和有关日志记录的实用工具和第三方库详解与示例
  • .NET6使用MiniExcel根据数据源横向导出头部标题及数据
  • .NET设计模式(7):创建型模式专题总结(Creational Pattern)
  • @Transactional 详解
  • [ Linux Audio 篇 ] 音频开发入门基础知识
  • [].slice.call()将类数组转化为真正的数组
  • [20160902]rm -rf的惨案.txt
  • [23] GaussianAvatars: Photorealistic Head Avatars with Rigged 3D Gaussians
  • [C#C++]类CLASS
  • [C/C++随笔] char与unsigned char区别