题目描述】

对于给定的两个字符串a,b,我们定义a*b是将把它们连接在一起形成的字符串。例如,若a="abc",b="def",则a*b="abcdef"。如果我们将这种运算视为乘法,则非负整数的乘方运算被以类似的方式定义:a^0=""(空字符串),a^(n+1)=a*(a^n)。

【输入格式】

输入包含多组数据。

每组数据有一行一个大写字母组成的字符串s,其长度至少为1,至多为10^6.输入结束标志为一行一个“.”(半角句号)。

【输出格式】

输出使得存在某个a,使得a^n=s的最大n。

【样例输入】

ABCD
AAAA
ABABAB
.

【样例输出】

1
4
3

题解:

容易题,可后缀数组被卡了常,于是上了KMP 显然答案即为 n/(n-next[n]+1)

 #include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const int N=1e6+;
char S[N];int n,s[N],c[],rk[N],x[N],y[N],sa[N],k,f[N][],high[N];
void Getsa(){
int m=,t;
for(int i=;i<=m;i++)c[i]=;
for(int i=;i<=n;i++)c[x[i]=s[i]]++;
for(int i=;i<=m;i++)c[i]+=c[i-];
for(int i=n;i>=;i--)sa[c[x[i]]--]=i;
for(k=;k<=n;k<<=){
t=;
for(int i=;i<=m;i++)y[i]=;
for(int i=n-k+;i<=n;i++)y[++t]=i;
for(int i=;i<=n;i++)if(sa[i]>k)y[++t]=sa[i]-k;
for(int i=;i<=m;i++)c[i]=;
for(int i=;i<=n;i++)c[x[i]]++;
for(int i=;i<=m;i++)c[i]+=c[i-];
for(int i=n;i>=;i--)sa[c[x[y[i]]]--]=y[i];
swap(x,y);
x[sa[]]=t=;
for(int i=;i<=n;i++)x[sa[i]]=(y[sa[i]]==y[sa[i-]] && y[sa[i]+k]==y[sa[i-]+k])?t:++t;
if(t==n)break;
m=t;
}
for(int i=;i<=n;i++)rk[sa[i]]=i;
}
void Gethight(){
int h=,j;
for(int i=;i<=n;i++){
j=sa[rk[i]-];
if(h)h--;
for(;i+h<=n && j+h<=n;h++)if(s[i+h]!=s[j+h])break;
high[rk[i]-]=h;
}
}
void prework(){
int tmp,to;
for(int i=;i<=n;i++)f[i][]=high[i];
for(int j=;j<=;j++){
tmp=n-(<<j)+;
for(int i=,to;i<=tmp;i++){
to=i+(<<(j-));
if(f[i][j-]<f[to][j-])f[i][j]=f[i][j-];
else f[i][j]=f[to][j-];
}
}
}
int query(int l,int r){
int t=log(r-l+)/log();
return min(f[l][t],f[r-(<<t)+][t]);
}
int lcp(int i,int j){
if(rk[i]>rk[j])swap(i,j);
return query(rk[i],rk[j]-);
}
void Getanswer(){
int t,pp=n>>;
for(int L=;L<=pp;L++){
if(n%L)continue;
t=lcp(,+L);
if(t==n-L){
printf("%d\n",n/L);
return ;
}
}
printf("1\n");
}
void work(){
n=strlen(S+);
for(int i=;i<=n;i++)s[i]=S[i]-'A'+;
Getsa();Gethight();prework();
Getanswer();
}
int main()
{
freopen("powerstrings.in","r",stdin);
freopen("powerstrings.out","w",stdout);
while(scanf("%s",S+)){
if(S[]=='.')break;
work();
}
return ;
}

80分后缀数组

 #include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const int N=1e6+;
int net[N],n;char s[N];
void work(){
n=strlen(s+);int t;
net[]=;
for(int i=;i<=n;i++){
t=net[i-];
while(t!= && s[t]!=s[i])
t=net[t-];
if(s[t]==s[i])net[i]=t+;
else net[i]=;
}
if(!(n%(n-net[n]+)))
printf("%d\n",n/(n-net[n]+));
else printf("1\n");
}
int main()
{
freopen("powerstrings.in","r",stdin);
freopen("powerstrings.out","w",stdout);
while(scanf("%s",s+)){
if(s[]=='.')break;
work();
}
return ;
}

AC KMP

[POJ2406]字符串的幂的更多相关文章

  1. COGS 1710. [POJ2406]字符串的幂

    ★☆   输入文件:powerstrings.in   输出文件:powerstrings.out   简单对比时间限制:3 s   内存限制:256 MB [题目描述] 对于给定的两个字符串a,b, ...

  2. UVA 10298 Power Strings 字符串的幂(KMP,最小循环节)

    题意: 定义a为一个字符串,a*a表示两个字符相连,即 an+1=a*an ,也就是出现循环了.给定一个字符串,若将其表示成an,问n最大为多少? 思路: 如果完全不循环,顶多就是类似于abc1这样, ...

  3. bzoj4002 [JLOI2015]有意义的字符串 快速幂

    Description B 君有两个好朋友,他们叫宁宁和冉冉. 有一天,冉冉遇到了一个有趣的题目:输入 b;d;n,求((b+sqrt(D)/2)^N的整数部分,请输出结果 Mod 752844341 ...

  4. POJ-2406(KMP+字符串压缩)

    Power String POJ-2406 字符串压缩模板题,但是是求有多少个这样最短的子串可以组成s. #include<iostream> #include<cstring> ...

  5. 牛客国庆集训派对Day2 Solution

    A    矩阵乘法 思路: 1° 牛客机器太快了,暴力能过. #include <bits/stdc++.h> using namespace std; #define N 5000 in ...

  6. 51Nod 1873 初中的算术

    大神的字符串快速幂 #include <iostream> #include <string> #include <algorithm> #include < ...

  7. java数据结构和算法09(哈希表)

    树的结构说得差不多了,现在我们来说说一种数据结构叫做哈希表(hash table),哈希表有是干什么用的呢?我们知道树的操作的时间复杂度通常为O(logN),那有没有更快的数据结构?当然有,那就是哈希 ...

  8. 3.24 7.13 Python基础汇总

    对象类型 类型名称 示例 简要说明 备注 数字 int,float,complex 1234,3.14,1.3e5,3+4j 数字大小没有限制 十六进制用0x前缀和0-9,a-f表示 字符串 str ...

  9. 【BZOJ4002】[JLOI2015]有意义的字符串(数论,矩阵快速幂)

    [BZOJ4002][JLOI2015]有意义的字符串(数论,矩阵快速幂) 题面 BZOJ 洛谷 题解 发现我这种题总是做不动... 令\(A=\frac{b+\sqrt d}{2},B=\frac{ ...

随机推荐

  1. 树莓派3启动wifi并且配置wifi

    概述 树莓派3内置了wifi和蓝牙模块,我们不用像以前的版本那样,再去购买一个外接的模块练到raspberry上. 当我们第一次启动了树莓派的时候,必然使用了网线,但是之后的每一次使用,我们当然更希望 ...

  2. Java ftp 上传文件和下载文件

    今天同事问我一个ftp 上传文件和下载文件功能应该怎么做,当时有点懵逼,毕竟我也是第一次,然后装了个逼,在网上找了一段代码发给同事,叫他调试一下.结果悲剧了,运行不通过.(装逼失败) 我找的文章链接: ...

  3. HP DL380服务器RAID信息丢失数据恢复方法和数据恢复过程分享

    [数据恢复故障描述]    客户服务器属于HP品牌DL380系列,存储是由6块73GB SAS硬盘组成的RAID5,操作系统是WINDOWS 2003 SERVER,主要作为企业部门内部的文件服务器来 ...

  4. nyoj n-1位数

    n-1位数 时间限制:3000 ms  |  内存限制:65535 KB 难度:1   描述 已知w是一个大于10但不大于1000000的无符号整数,若w是n(n≥2)位的整数,则求出w的后n-1位的 ...

  5. EasyUi中对话框。

    html页面代码: <head id="Head1" runat="server"> <meta http-equiv="Conte ...

  6. JAVA_SE基础——48.多态

    面向对象程序设计的三个特点是封装.继承和多态.前面已经学习了前两个特点.本章节将介绍多态性. 多态:一个对象具备多种形态.(父类的引用类型变量指向了子类的对象)或者是接口 的引用类型变量指向了接口实现 ...

  7. Oracle10g物理DG详细配置方法及步骤

    --测试环境:    OS:Redhat linux(64)    Primary:    IP:192.168.94.198    SID:dgdb1    Hostname:dg1    DB_U ...

  8. 新概念英语(1-121)The man in a hat

    Why didn't Caroline recognize the customer straight away ?A:I bought two expensive dictionaries here ...

  9. zuul入门(3)开发zuul的过滤器

    1.编写Zuul过滤器(Java&Groovy) 理解过滤器类型和请求生命周期后,我们来编写一个Zuul过滤器.编写Zuul的过滤器非常简单,我们只需继承抽象类ZuulFilter,然后实现几 ...

  10. centOs6.5配置jdk及其注意事项

    1.下载jdk1.7 百度云链接: https://pan.baidu.com/s/15vXLO2eV18eVvmt-R5jGnQ 密码: 1gd6 2.解压压缩包 通过终端在/usr/local下新 ...