KMP + 求最小循环节 --- HUST 1010 - The Minimum Length
2015-07-28 08:05
423 查看
The Minimum Length
Problem's Link: http://acm.hust.edu.cn/problem/show/1010
[b]Mean:[/b]给你一个字符串,求这个字符串的最小循环节。
[b]analyse:[/b]
KMP之next数组的运用。裸的求最小循环节。
[b]Time complexity: O(N)[/b]
[b]Source code: [/b]
/*
* 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;f
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);
while(~scanf("%s",s))
{
n=strlen(s);
getNext();
int min_cycle=n-Next[n-1];
printf("%d\n",min_cycle);
}
return 0;
}
/*
*/
相关文章推荐
- OC语言第四课 Foundation框架一——iOS学习连载九
- OC语言第五课 Foundation框架二——iOS学习连载十
- NY 2 括号配对问题 【栈】
- ASPX和Razor
- pagerank收敛原因
- 数据库收缩:NOTRUNCATE与TRUNCATEONLY
- UIPickerView与UIDatePicker
- Foundation框架----字符串、数组连载三
- hdoj 1526 A Plug for UNIX 【floyd求传递闭包 + 二分图匹配】
- Dos 常用命令整理
- Android - ViewHolder优化ListView
- Swift遍历数组的三种方式
- 程序员如何爱护眼睛
- Python good tutorials
- Java 性能优化系列之1[设计与程序优化]
- UVA 10050 Hartals
- 据说最近IMO中国队失利的一题
- 文件I/O
- 文件I/O
- C:冒泡排序