POJ2406-Power Strings(kmp循环节)
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 31111 | Accepted: 12982 |
Description
empty string) and a^(n+1) = a*(a^n).
Input
Output
Sample Input
abcd
aaaa
ababab
.
Sample Output
1
4
3
题意:输出循环节最大的个数
思路:next函数推断一下就可以:
若n%(n-next[n])==0 ans = n/(n-next[n])
否则 ans = 1
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 1000000+10;
int next[maxn];
char str[maxn];
int n;
void getNext(){
next[0] = next[1] = 0;
for(int i = 1,j; i < n; i++){
j = next[i];
while(j && str[i] != str[j]) j = next[j];
if(str[i]==str[j]) next[i+1] = j+1;
else next[i+1] = 0;
}
}
int main(){ while(~scanf("%s",str) && strcmp(str,".")!=0){
n = strlen(str);
getNext();
int ans;
if(n%(n-next[n])!=0) ans = 1;
else ans = n/(n-next[n]);
printf("%d\n",ans);
}
return 0;
}
POJ2406-Power Strings(kmp循环节)的更多相关文章
- POJ2406 Power Strings —— KMP or 后缀数组 最小循环节
题目链接:https://vjudge.net/problem/POJ-2406 Power Strings Time Limit: 3000MS Memory Limit: 65536K Tot ...
- poj2406 Power Strings (kmp 求最小循环字串)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 47748 Accepted: 19902 ...
- POJ2406 Power Strings(KMP)
Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 56162 Accepted: 23370 Description Giv ...
- poj 2406 Power Srings (kmp循环节) (经典)
<题目链接> 题目大意: 给出一个字符串,求其字串在该字符串中循环的最大周期. 解题分析: length=len-Next[len],len为该字符串的最小循环节,如果len%length ...
- POJ2406 Power Strings KMP算法
给你一个串s,如果能找到一个子串a,连接n次变成它,就把这个串称为power string,即a^n=s,求最大的n. 用KMP来想,如果存在的话,那么我每次f[i]的时候退的步数应该是一样多的 譬 ...
- POJ2406 Power Strings(KMP,后缀数组)
这题可以用后缀数组,KMP方法做 后缀数组做法开始想不出来,看的题解,方法是枚举串长len的约数k,看lcp(suffix(0), suffix(k))的长度是否为n- k ,若为真则len / k即 ...
- poj2406 Power Strings(kmp)
poj2406 Power Strings(kmp) 给出一个字符串,问这个字符串是一个字符串重复几次.要求最大化重复次数. 若当前字符串为S,用kmp匹配'\0'+S和S即可. #include & ...
- poj2406 Power Strings(kmp失配函数)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 39291 Accepted: 16315 Descr ...
- poj2406 Power Strings 【KMP】
Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc&quo ...
- POJ2406 Power Strings 【KMP 或 后缀数组】
电源串 时间限制: 3000MS 内存限制: 65536K 提交总数: 53037 接受: 22108 描述 给定两个字符串a和b,我们定义a * b是它们的连接.例如,如果a =" ...
随机推荐
- ALL运算符
ALL在英文中的意思是“所有”,ALL运算符要求比较的值需要匹配子查询中的所有值.ALL运算符同样不能单独使用,必须和比较运算符共同使用. 下面的SQL语句用来检索在所有会员入会之前出版的图书: SE ...
- 如何使用weinre来进行远程调试phonegap应用
使用phonegap开发的应用在真机上和PC上的显示效果以及浏览器渲染方式还是有些区别的.在PC端很好调试,各种浏览器都自带了调试工具,使用起来很方便,但是在一旦安装到了手机上,这个时候要进行调试就需 ...
- WebService协议
http://www.cnblogs.com/lm3515/archive/2011/03/17/1987009.html http://blog.csdn.net/chjttony/article/ ...
- 关于SOA
什么是SOA SOA:面向服务的体系结构(Service-Oriented Architecture,SOA,也叫面向服务架构), SOA是指为了解决在Internet环境下业务集成的需要,通过连接能 ...
- 【动态规划/递推】BZOJ1806[IOI2007]- Miners
IOI历史上的著名水题,我这种蒟蒻都能写的东西. [思路] 用1.2.3分别代替三种食物,0表示当前矿井没有食物.f[i][a][b][c][d]当前第i个食物,矿1的食物顺序由上至下为a,b:矿2的 ...
- Problem D: 调用自定义函数search(int list[], int n),在数组中查找某个数
AC代码#include <stdio.h> int find(int *a, int l, int x) { ; int i; ; i < l; i ++) if(a[i] == ...
- (转载)JavaScript中匿名函数,函数直接量和闭包
首先,我们先看看下面几种写法:1.function f(x){return x*x;};f(x);2.(function(x){return x*x;})(x);3.(function(x){retu ...
- 我告诉你MSDN
https://msdn.itellyou.cn/ 微软的软件,系统,官方下载
- sql server的sql 语句中的列名包含[]时候,把]替换成]]就可以
sql server的sql 语句中的列名包含[]时候,把]替换成]]就可以eg: create table p.e_LOG_WebServer ( [BSCFlg] int, ), ) ); sel ...
- DockManager
Devexpress----DockManager类似VS左右上下浮动栏停靠DockManager->Customize->DockPanel->NEW->Text='详细': ...