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.

  题目就是给一个字符串,然后让对从2到N的所有前缀字符串,找出里面的重复串。

  对于找重复串的问题,用扩展KMP就可以解决。然后就是对于每个前缀,可以证明后面的前缀的最小重复串的长度一定大于等于前面的,因为形成重复串的必要条件就是i+next1[i]>=length 这样的话如果前面不符合这个式子,后面就更不用说了,必须让i增大才可能。

代码如下:

// ━━━━━━神兽出没━━━━━━
// ┏┓ ┏┓
// ┏┛┻━━━━━━━┛┻┓
// ┃ ┃
// ┃ ━ ┃
// ████━████ ┃
// ┃ ┃
// ┃ ┻ ┃
// ┃ ┃
// ┗━┓ ┏━┛
// ┃ ┃
// ┃ ┃
// ┃ ┗━━━┓
// ┃ ┣┓
// ┃ ┏┛
// ┗┓┓┏━━━━━┳┓┏┛
// ┃┫┫ ┃┫┫
// ┗┻┛ ┗┻┛
//
// ━━━━━━感觉萌萌哒━━━━━━ // Author : WhyWhy
// Created Time : 2015年07月18日 星期六 10时01分22秒
// File Name : 1961.cpp #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> using namespace std; const int MaxN=; void EKMP_pre(int m,char s[],int next1[])
{
int p=,a=,L; next1[]=m; while(p+<m && s[p]==s[p+])
++p; next1[]=p; for(int k=;k<m;++k)
{
L=next1[k-a];
p=next1[a]+a-(next1[a]!=); if(k+L-<p)
next1[k]=L;
else
{
++p; while(p<m && s[p]==s[p-k])
++p; next1[k]=p-k;
a=k;
}
} next1[m]=;
} int next1[MaxN];
char s[MaxN];
int N; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int cas=;
int minn; while(~scanf("%d",&N) && N)
{
minn=;
scanf("%s",s); EKMP_pre(N,s,next1); printf("Test case #%d\n",cas++); for(int i=;i<N;++i)
{
while(minn<=i && next1[minn]+minn<i+)
++minn; if(minn<=i && (i-minn+)%minn==)
printf("%d %d\n",i+,(i-minn+)/minn+);
} puts("");
} return ;
}

最新文章

  1. Android studio .gitignore 文件的内容
  2. kail2 linux 安装vmware tools
  3. java攻城狮之路(Android篇)--ListView与ContentProvider
  4. 【MPI学习6】MPI并行程序设计模式:具有不连续数据发送的MPI程序设计
  5. Delphi中SQL批量插入记录
  6. linux win 的换行转换
  7. 内核参数优化/etc/sysctl.conf
  8. python二进制文件解析
  9. [React] React Router: setRouteWillLeaveHook
  10. EFcore与动态模型(三)
  11. 基于JerseyToken安全设计
  12. 【UVa11426】GCD - Extreme (II)(莫比乌斯反演)
  13. whois 查询 API
  14. python--日志模块
  15. 存储过程导入excel
  16. [noip2017] 前三周总结
  17. Error【0007】:zabbix中因为curl版本过低而无法发送邮件
  18. java多线程快速入门(五)
  19. nvm管理不同版本的node和npm
  20. Python之输出当前时间

热门文章

  1. plsql 安装后database下拉没有东西
  2. Tab选项卡的原生写法
  3. 转载 Deep learning:六(regularized logistic回归练习)
  4. 查看log的方法
  5. java super关键字
  6. World Finals 1996 Uva 247 (Floyd求闭包)
  7. android 内存优化一
  8. javascript 总结学习一
  9. 转:WebDriver(Selenium2) 处理可能存在的JS弹出框
  10. php使用curl提交xml数据