E - Period

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 A K , that is A concatenated K times, for some string A. Of course, we also want to know the period K.

InputThe input file consists of several test cases. Each test case consists of two lines. The first one contains N (2 <= N <= 1 000 000) ?C 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. 
OutputFor 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

结论:j%(j-Nex[t])==0 说明该前缀是一个周期为j/(j-Next[j])的周期子序列证明: j - Next[j] 是子串在失配时候的右移长度,
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
#define MAXN 1000001
typedef long long LL;
/*
*/
char s[MAXN];
int Next[MAXN];
void kmp_pre(int m)
{
    int j,k;
    j = 0;
    k = -1;
    Next[0] = -1;
    while(j<m)
    {
        if(k==-1||s[j]==s[k])
            Next[++j] = ++k;
        else
            k = Next[k];
    }
}
int main()
{
    int m;
    int cas=1;
    while(scanf("%d",&m),m)
    {
        scanf("%s",s);
        kmp_pre(m);
        printf("Test case #%d\n",cas++);
        for(int i=1;i<=m;i++)
            if((i)%(i-Next[i])==0&&i/(i-Next[i])>1)
                printf("%d %d\n",i,(i)/(i-Next[i]));
        cout<<endl;
    }
}
时间: 2024-10-13 13:44:20

E - Period的相关文章

今天的工作状态,规划未来一段时间内必须完成的事情(Record the working status of today,planning for the next period of time must be completed)

中文: 今天的工作状态,规划未来一段时间内必须完成的事情 待完成功能:(本周完成,不包括modbus传感器,完成之后就不管了) 1.传感器识别功能框架: 根据四个上拉电阻自动识别工作模式:数字型传感器.模拟形传感器.modebus式传感器 2.类似于红外的FD把STM32远程升级功能实现(思考实现方法,如此大的程序,分段存储吗?待处理)3.基于Zigbee的485的透传实现 业余生活: 1.把以上功能实现,ESP8266的AT指令掌握使用,然后基于Linux开发简单的功能(基础) 2.把TI的C

POJ1961 Period

Period Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 18405   Accepted: 8920 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

POJ Period 1961【KMP】

Language: Default Period Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 14658   Accepted: 6968 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 t

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

Period Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 14653   Accepted: 6965 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

POJ 1961 Period

Period Time Limit: 3000ms Memory Limit: 30000KB This problem will be judged on PKU. Original ID: 196164-bit integer IO format: %lld      Java class name: Main For each prefix of a given string S with N characters (each character has an ASCII code bet

Configure Windows Server 2008 based DHCP database cleanup interval & lease grace period

My Windows Server 2008 based DHCP server related settings: 这里,LeaseExtension设置为10 minutes,意味着默认4个小时的grace period缩短到10分钟.DatabaseCleanupInterval默认为60 minutes,即默认每小时执行一次DHCP database cleanup. Configure Windows Server 2008 based DHCP database cleanup in

Period(sdut2476)

[题目大意]:给定一个字符串,求到哪一位时的字串是前几位循环组成的,并求出循环次数. 思路:求每个前缀的最小循环周:从i到n枚举len,如果len%(len-next[len])==0,则这个前缀是由循环节组成的,且循环次数为len/(len-next[len])//len为当前i的值,next[len]为当前j的值.#include <stdio.h> #include <string.h> #include <stdlib.h> char a[1000001]; i

POJ——T 1961 Period

http://poj.org/problem?id=1961 Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 18542   Accepted: 9007 Description For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we

Period HDU - 1358

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 (

hdu_5908_Abelian Period(暴力)

题目链接:hdu_5908_Abelian Period 题意: 给你n个数字,让你找出所有的k,使得把这n个数字分为k分,并且每份的数字种类和个数必须相同 题解: 枚举k,首先k必须是n的约数,然后就能算出每个数字应该出现多少次,O(n)检验即可. 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;++i) 3 using namespace std; 4 5 const int N=1e5+7; 6 int t