Period

POJ - 1961
时限: 3000MS   内存: 30000KB   64位IO格式: %I64d & %I64u

提交 状态

已开启划词翻译

问题描述

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.

输入

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.

输出

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.

样例输入

3
aaa
12
aabaabaabaab
0

样例输出

Test case #1
2 2
3 3 Test case #2
2 2
6 2
9 3
12 4

来源

题意:一个字符串的前缀,可以由其最小的循环节循环k(k>1, 次得到。按升序输出这样的前缀的长度。
 #include <iostream>
#include <cstdio>
#include <cstring> using namespace std; #define maxn 1000008 char s[maxn];
int next[maxn]; void getNext()
{
int j, k, i;
i = strlen(s); j = ;
k = -;
next[] = -;
while(j < i)
{
if(k == - || s[j] == s[k])
{
j++;
k++;
next[j] = k;
}
else
k = next[k];
}
} int main()
{
int q, p = ;
while(scanf("%d", &q), q)
{
scanf("%s", s); memset(next, , sizeof(next));
getNext(); printf("Test case #%d\n", p++); for(int i = ; i <= q; i++)
{
int k = next[i]; // 这个前缀的,前缀和后缀最长的相等长度
if(i % (i - k) == && i / (i - k) != ) // 满足循环节……循环 输出
printf("%d %d\n", i, i / (i - k));
}
puts("");
}
return ;
}

Period POJ - 1961的更多相关文章

  1. Match:Period(POJ 1961)

    Period 题目大意:给定一个字符串,要你找到前缀重复了多少次 思路,就是kmp的next数组的简单应用,不要修正next的距离就好了,直接就可以跳转了 PS:喝了点酒用递归实现除法和取余了...结 ...

  2. KMP POJ 1961 Period

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

  3. POJ 1961 2406 (KMP,最小循环节,循环周期)

    关于KMP的最短循环节.循环周期,请戳: http://www.cnblogs.com/chenxiwenruo/p/3546457.html (KMP模板,最小循环节) POJ 2406  Powe ...

  4. poj 1961 Period

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

  5. POJ 1961 Period( KMP )*

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

  6. POJ 1961 Period(KMP)

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

  7. POJ 1961 Period KMP算法next数组的应用

    题目: http://poj.org/problem?id=1961 很好的题,但是不容易理解. 因为当kmp失配时,i = next[i],所以错位部分就是i - next[i],当s[0]...s ...

  8. (简单) POJ 1961 Period,扩展KMP。

    Description For each prefix of a given string S with N characters (each character has an ASCII code ...

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

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

随机推荐

  1. 转载Django 500,404,400错误修改优化

    转载:https://blog.csdn.net/qq_38038143/article/details/80105653 404错误:page not found视图 500错误:server er ...

  2. git比较重要但是又容易忘记的操作

    git rebase head~1   把多次commit合并成一次 git reset head  撤销已缓存的内容 git checkout . git stash      去除改动未提交的代码

  3. xml与Properties的区别

    1.properties配置文件,是一个属性对应于一个值(key = value)这样的键值匹对模式: 每一行properties配置文件的键值,对应着一次赋值: 特殊点: 在前后两行properti ...

  4. Java数据结构之队列(Queue)

    1.使用场景 银行排队的案例: 2.队列介绍 队列是一个有序列表,可以用数组或是链表来实现. 遵循先入先出的原则: 先存入队列的数据,要先取出. 后存入的要后取出 示意图:(使用数组模拟队列示意图) ...

  5. dfs(找环)

    https://codeforces.com/problemset/problem/1249/B2 B2. Books Exchange (hard version) time limit per t ...

  6. [集合Set]HashSet、LinkedHashSet TreeSet

    Set Set是不包含重复元素的集合.更正式地,集合不包含一对元素e1和e2,使得e1.equals(e2),并且最多一个空元素. 无索引,不可以重复,无序(存取不一致) Set接口除了继承自Coll ...

  7. Mysql日期和字符的相互转换

    今天从网上查到了一些关于MySQL数据库的日期转换函数的转换的用法,在这里记录一下: mysql日期和字符相互转换 date_format(date,'%Y-%m-%d') ------------- ...

  8. referenceQueue用法

    何为referenceQueue 在java的引用体系中,存在着强引用,软引用,虚引用,幽灵引用,这4种引用类型.在正常的使用过程中,我们定义的类型都是强引用的,这种引用类型在回收中,只有当其它对象没 ...

  9. mysql导出函数或者存储过程 设置显示方式

    mysql导出函数或者存储过程 mysqldump -hhostname -uusername -ppassword -ntd -R databasename > /app/backupflie ...

  10. 2019-5-28-VisualStudio-扩展开发

    title author date CreateTime categories VisualStudio 扩展开发 lindexi 2019-05-28 19:51:49 +0800 2018-2-1 ...