POJ2406 Power Strings —— KMP or 后缀数组 最小循环节
题目链接:https://vjudge.net/problem/POJ-2406
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 52631 | Accepted: 21921 |
Description
Input
Output
Sample Input
- abcd
- aaaa
- ababab
- .
Sample Output
- 1
- 4
- 3
Hint
Source
KMP:
求字符串的最小循环节。
1) 如果字符串长度能被“初步的最小循环节”整除,那么这个就是他的最小循环节。
2) 如果字符串长度不能被“初步的最小循环节”整除,那么这个只是它通过补全后的最小循环节。而它实际的最小循环节为自己。
代码如下:
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cstdlib>
- #include <string>
- #include <vector>
- #include <map>
- #include <set>
- #include <queue>
- #include <sstream>
- #include <algorithm>
- using namespace std;
- typedef long long LL;
- const double eps = 1e-;
- const int INF = 2e9;
- const LL LNF = 9e18;
- const int MOD = 1e9+;
- const int MAXN = 2e6+;
- char x[MAXN];
- int Next[MAXN];
- void get_next(char x[], int m)
- {
- int i, j;
- j = Next[] = -;
- i = ;
- while(i<m)
- {
- while(j!=- && x[i]!=x[j]) j = Next[j];
- Next[++i] = ++j;
- }
- }
- int main()
- {
- while(scanf("%s", x) && strcmp(x, "."))
- {
- int len = strlen(x);
- get_next(x, len);
- int r = len-Next[len]; //求出最小循环节
- if(len%r) //如果长度/最小循环节除不尽,则对于此字符串来说,实际的最小循环节是自己
- printf("1\n");
- else
- printf("%d\n", len/r);
- }
- }
后缀数组:
POJ2406 Power Strings —— KMP or 后缀数组 最小循环节的更多相关文章
- codeforces 825F F. String Compression dp+kmp找字符串的最小循环节
/** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: d ...
- POJ2406 Power Strings(KMP,后缀数组)
这题可以用后缀数组,KMP方法做 后缀数组做法开始想不出来,看的题解,方法是枚举串长len的约数k,看lcp(suffix(0), suffix(k))的长度是否为n- k ,若为真则len / k即 ...
- poj 2406 Power Strings (kmp 中 next 数组的应用||后缀数组)
http://poj.org/problem?id=2406 Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submiss ...
- poj2406 Power Strings (kmp 求最小循环字串)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 47748 Accepted: 19902 ...
- Power Strings POJ - 2406 后缀数组
Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc&quo ...
- POJ2406 Power Strings(KMP)
Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 56162 Accepted: 23370 Description Giv ...
- POJ2406 Power Strings KMP算法
给你一个串s,如果能找到一个子串a,连接n次变成它,就把这个串称为power string,即a^n=s,求最大的n. 用KMP来想,如果存在的话,那么我每次f[i]的时候退的步数应该是一样多的 譬 ...
- Codeforces Round #146 (Div. 1) C - Cyclical Quest 后缀自动机+最小循环节
#include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk mak ...
- 【kmp算法】poj2406 Power Strings
如果next[n]<n/2,一定无解. 否则,必须要满足n mod (n-next[n]) = 0 才行,此时,由于next数组的性质,0~n-next[n]-1的部分一定是最小循环节. [ab ...
随机推荐
- Codeforces 553D Nudist Beach(二分答案 + BFS)
题目链接 Nudist Beach 来源 Codeforces Round #309 (Div. 1) Problem D 题目大意: 给定一篇森林(共$n$个点),你可以在$n$个点中选择若干个构 ...
- Python通用编程
本文是Python通用编程系列教程,已全部更新完成,实现的目标是从零基础开始到精通Python编程语言.本教程不是对Python的内容进行泛泛而谈,而是精细化,深入化的讲解,共5个阶段,25章内容.所 ...
- ssm实现查看流程图
jsp <!--显示数据列表--> <tbody id="TableData" class="dataContainer" datakey=& ...
- Silverlight 离线安装包
直接下载地址 https://www.microsoft.com/getsilverlight/locale/en-us/html/Microsoft%20Silverlight%20Release% ...
- Xcode: This device is no longer connected error
Quit the xcode and connect again will all right.
- 【redis】5.spring boot项目中,直接在spring data jpa的Repository层使用redis +redis注解@Cacheable直接在Repository层使用,报错问题处理Null key returned for cache operation
spring boot整合redis:http://www.cnblogs.com/sxdcgaq8080/p/8028970.html 首先,明确一下问题的场景 之前在spring boot整合re ...
- 【Tensorflow】tf.argmax函数
tf.argmax(input, axis=None, name=None, dimension=None) 此函数是对矩阵按行或列计算最大值 参数 input:输入Tensor axis:0表示 ...
- Proftpd快速搭建FTP服务器
前言 在Linux系统中,FTP服务器软件有很多,都已经成熟,像vsftpd, wu-ftp, Pure-FTPd等.但这些软件安装配置起来都比较麻烦,搭建个人的FTP服务器,还是Proftpd比较简 ...
- C#中异常处理和Java的区别
捕获异常,同样是try...catch...,这个完全一样: 抛出异常,同样是throw,这个完全一样: 函数抛出怎样的异常,Java中可以用throws定义,而C#中不用定义,相当于throws E ...
- Eclipse出现"Running Android Lint has encountered a problem"解决方式
近期打开Eclipse的时候,总是发生这种一个错误:"Running Android Lint has encountered a problem".截图例如以下: . 可是Ecl ...