Period

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3196    Accepted Submission(s): 1603

Problem 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 file 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
题目大意:一个字符串,问从头到某个位置,字符串的前缀最多重复了多少次。比方aaaa的字符串,到第二个字符,前缀a重复了两次,到第三个字符,前缀a重复了三次,到第四个字符,前缀a重复了四次,前缀aa重复了两次,但我们要得到的是重复了四次。
解题思路:考察KMP算法中的p数组。p数组是基本的,然后,在p数组中考察,p[6]=4,我们能知道的是,s[1]=s[3]=s[5],s[2]=s[4]=s[6],其实只要在p[k]=u时,当u整除(k-u)的时候,就是满足题目要求的时候。
 AC代码(一):
 #include <stdio.h>
#include <string.h>
#define N 1000000 int n;
char str[N];
int p[N]; void run(void)
{
memset(p,-,sizeof(p));
for(int i=;i<n;i++)
{
int k=p[i-];
while()
{
if(str[k+]==str[i])
{
p[i]=k+;
if((i+)%(i-p[i])==)
printf("%d %d\n",i+,(i+)/(i-p[i]));
break;
}
if(k==-)break;
k=p[k];
}
}
} int main()
{
int id=;
while(scanf("%d",&n)==&&n)
{
getchar();
gets(str);
printf("Test case #%d\n",++id);
run();
puts("");
}
}

AC代码(二):

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<string>
#include<cmath>
using namespace std;
const int M = 1e6+;
char s[M];
int next[M];
void solve()
{
int j=,k=-;
next[]= -;
while(s[j]!='\0')
{
if(k == -)
{
next[++j] = ;
k=;
}
if(s[k] == s[j])
{
k++;
next[++j] = k;
}
else k = next[k];
}
}
int main()
{
int n,id = ;
while(cin>>n && n)
{
scanf("%s",s);
int len = strlen(s);
printf("Test case #%d\n",++id);
solve();
for(int i=; i<=len; i++)
{
int j = i-next[i];
if(i%j == && i/j>)
printf("%d %d\n",i,i/j);
}
printf("\n");
}
return ;
}

HDU 1358 Period(kmp简单解决)的更多相关文章

  1. HDU 1358 Period KMP

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1358 求周期问题,简单KMP—— AC代码: #include <iostream> # ...

  2. hdu 1358 period KMP入门

    Period 题意:一个长为N (2 <= N <= 1 000 000) 的字符串,问前缀串长度为k(k > 1)是否是一个周期串,即k = A...A;若是则按k从小到大的顺序输 ...

  3. HDU 1358 Period(KMP计算周期)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目大意:给你一串字符串,判断字符串的前缀是否由某些字符串多次重复而构成. 也就是,从第1个字母 ...

  4. Hdu 1358 Period (KMP 求最小循环节)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目描述: 给出一个字符串S,输出S的前缀能表达成Ak的所有情况,每种情况输出前缀的结束位置和 ...

  5. hdu 1358 Period(KMP入门题)

    Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  6. HDU 1358 Period(KMP next数组运用)

    Period Problem Description For each prefix of a given string S with N characters (each character has ...

  7. [HDU 1358]Period[kmp求周期]

    题意: 每一个power前缀的周期数(>1). 思路: kmp的next. 每一个前缀都询问一遍. #include <cstring> #include <cstdio> ...

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

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

  9. HDU 3746 - Cyclic Nacklace & HDU 1358 - Period - [KMP求最小循环节]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3746 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

随机推荐

  1. [转]sa不能远程连接sql server 2008的解决办法

    本文转自:http://www.cnblogs.com/chendaoyin/archive/2012/08/25/2656900.html 方法: 开始->Microsoft SQL Serv ...

  2. c++类模板template中的typename使用方法-超级棒

    转载:https://blog.csdn.net/vanturman/article/details/80269081 如有问题请联系我删除: 目录 起因 typename的常见用法 typename ...

  3. 两款工控控件对比评测:Iocomp和ProEssentials

    对于程序员来说,要凭一己之力开发出漂亮逼真的工控仪表和工控图表是非常耗时间和精力的,那么使用专业的第三方控件就是不错的选择,不仅节约开发时间,降低了项目风险,最重要的是第三方控件写的程序更专业,工控图 ...

  4. java 利用JAX-RS快速开发RESTful 服务实例

    首先看web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns=" ...

  5. [Python爬虫] 之十三:Selenium +phantomjs抓取活动树会议活动数据

    抓取活动树网站中会议活动数据(http://www.huodongshu.com/html/index.html) 具体的思路是[Python爬虫] 之十一中抓取活动行网站的类似,都是用多线程来抓取, ...

  6. [Webpack] Detect Unused Code with Webpack and unused-files-webpack-plugin

    As you refactor and modify applications, it's difficult to manage and keep track of files as they be ...

  7. 近期写的一个控件——Well Swipe beta 1.0

    原文地址:http://blog.csdn.net/u013045971/article/details/51119507 近期花了大概一个半月的业余时间写的.从没有到有,中间也碰到了非常多的坑,一点 ...

  8. 运用Unity结合PolicyInjection实现拦截器[结合操作日志实例]

    上一篇文章我们通过Unity自身Unity.InterceptionExtension.IInterceptionBehavior实现一个有系统关异常日志记录:解决代码中到处充满的异常记录的代码: 本 ...

  9. Shell 基本运算符(转)

    Shell 和其他编程语言一样,支持多种运算符,包括: 算数运算符 关系运算符 布尔运算符 字符串运算符 文件测试运算符 原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 ...

  10. docker高级应用之cpu与内存资源限制(转)

    时间:2015-06-09 14:01:52      阅读:1581      评论:0      收藏:0      [点我收藏+] 标签:docker资源限制   docker cpu限制    ...