Power Strings
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 31111   Accepted: 12982

Description

Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the
empty string) and a^(n+1) = a*(a^n).

Input

Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.

Output

For each s you should print the largest n such that s = a^n for some string a.

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循环节)的更多相关文章

  1. POJ2406 Power Strings —— KMP or 后缀数组 最小循环节

    题目链接:https://vjudge.net/problem/POJ-2406 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Tot ...

  2. poj2406 Power Strings (kmp 求最小循环字串)

    Power Strings   Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 47748   Accepted: 19902 ...

  3. POJ2406 Power Strings(KMP)

    Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 56162   Accepted: 23370 Description Giv ...

  4. poj 2406 Power Srings (kmp循环节) (经典)

    <题目链接> 题目大意: 给出一个字符串,求其字串在该字符串中循环的最大周期. 解题分析: length=len-Next[len],len为该字符串的最小循环节,如果len%length ...

  5. POJ2406 Power Strings KMP算法

    给你一个串s,如果能找到一个子串a,连接n次变成它,就把这个串称为power string,即a^n=s,求最大的n. 用KMP来想,如果存在的话,那么我每次f[i]的时候退的步数应该是一样多的  譬 ...

  6. POJ2406 Power Strings(KMP,后缀数组)

    这题可以用后缀数组,KMP方法做 后缀数组做法开始想不出来,看的题解,方法是枚举串长len的约数k,看lcp(suffix(0), suffix(k))的长度是否为n- k ,若为真则len / k即 ...

  7. poj2406 Power Strings(kmp)

    poj2406 Power Strings(kmp) 给出一个字符串,问这个字符串是一个字符串重复几次.要求最大化重复次数. 若当前字符串为S,用kmp匹配'\0'+S和S即可. #include & ...

  8. poj2406 Power Strings(kmp失配函数)

    Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 39291 Accepted: 16315 Descr ...

  9. poj2406 Power Strings 【KMP】

    Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc&quo ...

  10. POJ2406 Power Strings 【KMP 或 后缀数组】

    电源串 时间限制: 3000MS   内存限制: 65536K 提交总数: 53037   接受: 22108 描述 给定两个字符串a和b,我们定义a * b是它们的连接.例如,如果a =" ...

随机推荐

  1. 04、Unity_声音管理器

    1.分享一个Unity中用于管理声音的声音管理器,适合于中小型项目,大项目就算了. 2.借鉴了很多的源码,最后修改完成,吸取百家之长,改为自己所用,哈哈. 3.源码奉上: /* * * 开发时间:20 ...

  2. Python开发基础-Day3-列表、元组和字典

    列表 列表定义:[]内以逗号分隔,按照索引,存放各种数据类型,每个位置代表一个元素 特性: 1.可存放多个值 2.可修改指定索引位置对应的值,可变 3.按照从左到右的顺序定义列表元素,下标从0开始顺序 ...

  3. 【其它算法】Face The Right Way

    Face The Right Way Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5278   Accepted: 246 ...

  4. 【线段树】hihocoder 1586 ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 I. Minimum

    题意:给你一个序列(长度不超过2^17),支持两种操作:单点修改:询问区间中最小的ai*aj是多少(i可以等于j). 只需要线段树维护区间最小值和最大值,如果最小值大于等于0,那答案就是minv*mi ...

  5. 【Trie】【kd-tree】计蒜客17122 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 I. Barty's Computer

    题意:2种操作:①往集合里添加一个串 ②给你四个小串a b c d,问你集合里有几个串S满足S=a+S1+b+c+S2+d的形式.S1 S2可以为空,并且a+S1+b=c+S2+d. 就搞四颗Trie ...

  6. MySql数据库理解

    在之前的面试过程中,有被问到很多次,关于MySQL数据库相关知识,其中有问到了解存储引擎,数据库优化等问题,问得一脸懵X,确实以前在学习的时候没有去深入了解过这一块儿,今天找到了相应的数据库视频,稍稍 ...

  7. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) A. Orchestra 水题

    A. Orchestra 题目连接: http://www.codeforces.com/contest/635/problem/A Description Paul is at the orches ...

  8. 在iOS项目中使用截图

    最近项目中要求将个人的信息生成一张图片,以名片的方式分享出去.由此就需要使用截图功能.需求如图: 代码如下:

  9. #Java Web累积#关于MUI的上滑和下拉加载

    其实按照MUI的文档去写,也没什么问题: JSP中: <%@ page contentType="text/html;charset=UTF-8" language=&quo ...

  10. 看看如何解决“SQL Server只能使用Windows身份登录,不能使用sa等Sql server身份进行登录”的问题

    今天安装Sql Server之后,出现SQL Server只能使用Windows身份登录,不能使用sa等Sql server身份进行登录的问题是由于sql server只设置了Windows身份验证, ...