Period

Period

题目大意:

有多组测试用例,第一行为字符串长度,第二行为字符串,则对字符串的每一个前缀,若存在循环节且循环次数>1,输出长度和最大的循环次数;

每组测试用例后空一行;

SampleInput

3

aaa

12

aabaabaabaab

0

SampleOutput

Test case #1

2 2

3 3

Test case #2

2 2

6 2

9 3

12 4

分析:KMP算法,判断错位部分能否成为最小循环节即可;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
#define freopen freopen("in.txt","r",stdin)
const int maxn=1e6+10;
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
inline ll read()
{
    ll x=0;int f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
int n,m,k,t,nxt[maxn],cas;
char a[maxn];
int main()
{
    int i,j;
    while(~scanf("%d",&n)&&n)
    {
        scanf("%s",a);
        nxt[0]=-1,j=-1;
        for(i=0;i<n;)
        {
            if(j==-1||a[i]==a[j])nxt[++i]=++j;
            else j=nxt[j];
        }
        printf("Test case #%d\n",++cas);
        for(i=2;i<=n;i++)
        {
            if(nxt[i]>0&&i%(i-nxt[i])==0)
                printf("%d %d\n",i,i/(i-nxt[i]));
        }
        puts("");
    }
    //system("Pause");
    return 0;
}
时间: 2024-11-09 12:28:24

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