这题可以看作POJ 1961 最小循环节的一个简化版本。某补习班广告贴里给出的两个指针的参考解法简直大误。

受POJ 1961的启发,把数组看作字串,观察可知,如果字串全部由循环节构成(包括最后一段是不完整循环节的情况),则字串刨去最后一个字符的最长匹配前缀为最小循环节。而“最后一个字符的最长匹配前缀”即为KPM里的pattern[length -1]。所以我们只需按标准KMP算法求一遍失配函数即可。

    int minimumCycleSection(vector<int> &a) {
size_t len = a.size();
if(len <= ) return len;
int pattern[len];
memset(pattern, , len * );
pattern[] = -;
for(size_t i = ; i < len; i++){
int j = pattern[i-]+;
while(a[i] != a[j] && j > ){
j = pattern[j-] + ;
}
pattern[i] = a[i] == a[j] ? j : -;
}
return len - pattern[len-] - ;
}

Lint Code 1365. Minimum Cycle Section的更多相关文章

  1. Lint Code——最多共线的点的个数

    题目链接:http://www.lintcode.com/zh-cn/problem/max-points-on-a-line/# 条件:给一个点数组 目标:求出共线的点的最多个数 实现:时间复杂度- ...

  2. The Minimum Cycle Mean in a Digraph 《有向图中的最小平均权值回路》 Karp

    文件链接 Karp在1977年的论文,讲述了一种\(O(nm)\)的算法,用来求有向强连通图中最小平均权值回路(具体问题请参照这里) 本人翻译(有删改): 首先任取一个节点 \(s\) ,定义 \(F ...

  3. sonar Lint ----code bad smell

    类名注释报黄: 去掉这段黄做法:alt+enter 本文参考: http://www.cnblogs.com/xxoome/p/6677170.html

  4. [转]Passing Managed Structures With Strings To Unmanaged Code Part 1

    1. Introduction. 1.1 Managed structures that contain strings are a common sight. The trouble is that ...

  5. FSM Code Generator

    FSM Code Generator is a script code generator for Finite State Machine, it has a viaual designer bas ...

  6. gradle gradlew 的使用

    jcenter() 仓库比 mavenCentral() 仓库快,因此最好将jcenter 放前面,这样下载速度最快. 使用本地软件仓库:repositories { flatDir { dirs ' ...

  7. Oracle E-Business Suite Maintenance Guide Release 12.2(Patching Utilities)

    更多内容参考: http://docs.oracle.com/cd/E51111_01/current/acrobat/122ebsmt.zip Oracle Patch Application As ...

  8. Sphinx 2.2.11-release reference manual

    1. Introduction 1.1. About 1.2. Sphinx features 1.3. Where to get Sphinx 1.4. License 1.5. Credits 1 ...

  9. hbase官方文档(转)

    FROM:http://www.just4e.com/hbase.html Apache HBase™ 参考指南  HBase 官方文档中文版 Copyright © 2012 Apache Soft ...

随机推荐

  1. this and super

    this 和 super 的区别:this, 先从本类找属性和方法,本类找不到再从父类找.super, 从父类找. this 和 super 都可以调用构造方法,所以this() 和 super() ...

  2. 数据库复制 Nacicate Premium

    之前都是“备份-还原”,抑或“导出-导入”.今天在将SqlServer中的数据导入到MySql中时发现了一个非常方便的方法,无需任何繁琐的配置和操作.废话少说,进入正题: 工具:Navicat Pre ...

  3. 使用截图工具FastStone Capture

    使用截图工具FastStone Capture -谨以此教程献给某位上进的测试人员- FastStone Capture是本人用过的windows平台上最好用的截图工具,界面简洁,功能强大,还支持屏幕 ...

  4. Linux内核态和用户态

    两张图说明Linux内核态和用户态之间的关系

  5. vue弹出框的封装

    依旧是百度不到自己想要的,就自己动手丰衣足食 弹出框做成单独的组件confirm.vue; <template> <transition name="mask-bg-fad ...

  6. 第八章 计时器(BEEPER2)

    /*------------------------------------- BEEPER2.C -- Timer Demo Program No.1 (c) Charles Petzold, 19 ...

  7. September 14th 2017 Week 37th Thursday

    Don't let the past steal your present. 别让过去悄悄偷走了我们的当下. We take what we can get and make the best of ...

  8. September 09th 2017 Week 36th Saturday

    Don't wait to be lonely, to recognize the value of a friend. 不要等到孤独了,才明白朋友的价值. Don't wait to be left ...

  9. September 08th 2017 Week 36th Friday

    Death is so terribly final, while life is full of possibilities. 死亡是冰冷可怕的绝境,而或者却充满了无限的可能. It isn't t ...

  10. ASP.NET MVC 4 RC的JS/CSS打包压缩功能

    打包(Bundling)及压缩(Minification)指的是将多个js文件或css文件打包成单一文件并压缩的做法,如此可减少浏览器需下载多个文件案才能完成网页显示的延迟感,同时通过移除JS/CSS ...