Period

Time Limit: 3000MS Memory Limit: 30000K

Description

For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the prefix is a periodic string. That is, for each i (2 <= i <= N) we want to know the largest K > 1 (if there is one) such that the prefix of S with length i can be written as AK ,that is A concatenated K times, for some string A. Of course, we also want to know the period K.

Input

The input consists of several test cases. Each test case consists of two lines. The first one contains N (2 <= N <= 1 000 000) – the size of the string S.The second line contains the string S. The input file ends with a line, having the

number zero on it.

Output

For each test case, output “Test case #” and the consecutive test case number on a single line; then, for each prefix with length i that has a period K > 1, output the prefix size i and the period K separated by a single space; the prefix sizes must be in increasing order. Print a blank line after each test case.

Sample Input

3

aaa

12

aabaabaabaab

0

Sample Output

Test case #1

2 2

3 3

Test case #2

2 2

6 2

9 3

12 4


  • 题意就是给你一个字符串,问分别以字符串中的每一个字符结尾是否可以形成循环节,可以形成多少个循环节。

  • 其实就是考察的一个next数组。


#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 1e6+100;
char s[maxn];
int next[maxn]; void cal_next()
{
int k;
next[0] = k = -1;
int len = strlen(s);
for(int i=1;i<len;i++)
{
while(k>-1 && s[i]!=s[k+1])
k = next[k];
if(s[i] == s[k+1])
k++;
next[i] = k;
}
} void solve()
{
int len = strlen(s);
for(int i=1;i<len;i++)
{
if(next[i] == -1)
continue;
if((i+1)%(i + 1 - next[i] - 1) == 0)//形成循环节是一定没有余数的情况下
printf("%d %d\n",i+1,(i+1)/(i+1-next[i]-1));
}
} int main()
{
int n,t = 1;
while(scanf("%d",&n) && n)
{
scanf("%s",s);
printf("Test case #%d\n",t++);
cal_next();
solve();
printf("\n");
}
return 0;
}

POJ:1961-Period(寻找字符串循环节)的更多相关文章

  1. KMP POJ 1961 Period

    题目传送门 /* 题意:求一个串重复出现(>1)的位置 KMP:这简直和POJ_2406没啥区别 */ /******************************************** ...

  2. poj 1961 Period【求前缀的长度,以及其中最小循环节的循环次数】

    Period Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 14653   Accepted: 6965 Descripti ...

  3. poj 1961 Period 【KMP-next前缀数组的应用】

    题目地址:http://poj.org/problem?id=1961 Sample Input 3 aaa 12 aabaabaabaab 0 Sample Output Test case #1 ...

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

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

  5. HDU 1358 Period (kmp求循环节)(经典)

    <题目链接> 题目大意: 意思是,从第1个字母到第2字母组成的字符串可由某一周期性的字串(“a”) 的两次组成,也就是aa有两个a组成: 第三行自然就是aabaab可有两个aab组成: 第 ...

  6. FZU 1901 Period II(KMP循环节+公共前后缀)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1901 题目大意:题目大意求出所有p满足s[i]=s[i+p](i<=len-p) 解题思路: 其实就是要 ...

  7. POJ 1961 Period(KMP)

    http://poj.org/problem?id=1961 题意 :给你一个字符串,让你输出到第几个字符时,循环结的个数. 思路 :这个题和2409差不多,稍微修改一下,加一个循环就行了,用的也是K ...

  8. poj 1961 Period

    Period http://poj.org/problem?id=1961 Time Limit: 3000MS   Memory Limit: 30000K       Description Fo ...

  9. KMP——POJ-3461 Oulipo && POJ-2752 Seek the Name, Seek the Fame && POJ-2406 Power Strings && POJ—1961 Period

    首先先讲一下KMP算法作用: KMP就是来求在给出的一串字符(我们把它放在str字符数组里面)中求另外一个比str数组短的字符数组(我们叫它为ptr)在str中的出现位置或者是次数 这个出现的次数是可 ...

  10. POJ 1961 Period( KMP )*

    Period Time Limit: 3000MSMemory Limit: 30000K Total Submissions: 12089Accepted: 5656 Description For ...

随机推荐

  1. 066 Plus One

    给定一个非负整数组成的非空数组,给整数加一.可以假设整数不包含任何前导零,除了数字0本身.最高位数字存放在列表的首位.详见:https://leetcode.com/problems/plus-one ...

  2. 牛客网Java刷题知识点之子类继承不了父类里的(private属性、private方法、构造方法)

    不多说,直接上干货! 子类可以继承父类的属性和方法,除了那些private的外还有一样是子类继承不了的---构造器.对于构造器而言,它只能够被子类调用,而不能被子类继承. 调用父类的构造方法我们使用s ...

  3. springboot在lunix后台启动,退出账号也不关闭

    首先需要进到自己springboot项目的根目录,然后执行如下linux命令 nohup java -jar 自己的springboot项目.jar >日志文件名.log 2>&1 ...

  4. 整理一些最近项目中用到的一些JS方法

    验证是否为图片格式 function IsImgType(src) { var rFilter = /^(?:image\/bmp|image\/cis\-cod|image\/gif|image\/ ...

  5. .net memcache

    非常感谢csdn及冷月宫主让我很快学会了.net操作 memcache 文章转自:http://download.csdn.net/detail/e_wsq/4358982 C#存取Memcache的 ...

  6. spring中自动装配bean

    首先用@Component注解类: package soundsystem: import org.springframework.stereotype.Component; @Component p ...

  7. CF1088D Ehab and another another xor problem

    思路: 根据异或的性质一位一位来搞.参考了https://blog.lucien.ink/archives/362/ 实现: #include <bits/stdc++.h> using ...

  8. django之模版的自定义函数

    - 自定义函数 simple_tag a. app下创建templatetags目录 b. 任意xxoo.py文件 c. 创建template对象 register d. __author__ = ' ...

  9. 为什么要用mallloc

    为什么要用malloc函数申请内存空间? 有的程序往往在运行时才知道要动态分配多大的内存,例如: void foo(char *str, int n) { char buf[?]; strncpy(b ...

  10. SharePoint 2013 安装配置(3-1)

    在第二部分中,我向您展示了如何在Windows Server 2012 R2 for SharePoint 2013上设置Active Directory域服务.现在我们应该能够在Active Dir ...