[POJ2406]字符串的幂
题目描述】
对于给定的两个字符串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]字符串的幂的更多相关文章
- COGS 1710. [POJ2406]字符串的幂
★☆ 输入文件:powerstrings.in 输出文件:powerstrings.out 简单对比时间限制:3 s 内存限制:256 MB [题目描述] 对于给定的两个字符串a,b, ...
- UVA 10298 Power Strings 字符串的幂(KMP,最小循环节)
题意: 定义a为一个字符串,a*a表示两个字符相连,即 an+1=a*an ,也就是出现循环了.给定一个字符串,若将其表示成an,问n最大为多少? 思路: 如果完全不循环,顶多就是类似于abc1这样, ...
- bzoj4002 [JLOI2015]有意义的字符串 快速幂
Description B 君有两个好朋友,他们叫宁宁和冉冉. 有一天,冉冉遇到了一个有趣的题目:输入 b;d;n,求((b+sqrt(D)/2)^N的整数部分,请输出结果 Mod 752844341 ...
- POJ-2406(KMP+字符串压缩)
Power String POJ-2406 字符串压缩模板题,但是是求有多少个这样最短的子串可以组成s. #include<iostream> #include<cstring> ...
- 牛客国庆集训派对Day2 Solution
A 矩阵乘法 思路: 1° 牛客机器太快了,暴力能过. #include <bits/stdc++.h> using namespace std; #define N 5000 in ...
- 51Nod 1873 初中的算术
大神的字符串快速幂 #include <iostream> #include <string> #include <algorithm> #include < ...
- java数据结构和算法09(哈希表)
树的结构说得差不多了,现在我们来说说一种数据结构叫做哈希表(hash table),哈希表有是干什么用的呢?我们知道树的操作的时间复杂度通常为O(logN),那有没有更快的数据结构?当然有,那就是哈希 ...
- 3.24 7.13 Python基础汇总
对象类型 类型名称 示例 简要说明 备注 数字 int,float,complex 1234,3.14,1.3e5,3+4j 数字大小没有限制 十六进制用0x前缀和0-9,a-f表示 字符串 str ...
- 【BZOJ4002】[JLOI2015]有意义的字符串(数论,矩阵快速幂)
[BZOJ4002][JLOI2015]有意义的字符串(数论,矩阵快速幂) 题面 BZOJ 洛谷 题解 发现我这种题总是做不动... 令\(A=\frac{b+\sqrt d}{2},B=\frac{ ...
随机推荐
- [Android]上传到多个Maven仓库的Gradle插件RapidMavenPushPlugin
博客搬迁至https://blog.wangjiegulu.com RSS订阅:https://blog.wangjiegulu.com/feed.xml RapidMavenPushPlugin 用 ...
- css精简命名
想写写前言啥的,发现自己是前言无能星人. 简单吐吐槽好了,来到新公司,接手公司之前的项目,我想着也就是改改bug,慢慢来吧,粗略看了看这个项目的代码,目前仅看了html和css样式的,忍不住吐血三升. ...
- openfalcon
一.环境准备 操作系统:centos7(minimal,www.centos.org下载的包是CentOS-7-x86_64-Minimal-1611.iso) 1.1 更换阿里yum(个人习惯) 步 ...
- Angular组件——组件生命周期(二)
一.view钩子 view钩子有2个,ngAfterViewInit和ngAfterViewChecked钩子. 1.实现ngAfterViewInit和ngAfterViewChecked钩子时注意 ...
- python API验证
API验证 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 API验证: a. 发令牌: 静态 PS: 隐患 key ...
- Struts(十五):主题
Theme主题是配置的struts-core.jar下的com.apache.struts2包下的default.properties中(默认配置为:xhtml) ### Standard UI th ...
- 关于terraform的状态管理
我们想在aws创建3台主机,使用ansible和terraform都是可以实现的. 用ansible可能是这样子的: - ec2: count: 10 image: ami-40d281120 ins ...
- jquery中的attr()与prop()的区别
根据官方的建议:具有 true 和 false 两个属性的属性,如 checked, selected 或者 disabled 使用prop(),其他的使用 attr()
- Django REST framework+Vue 打造生鲜超市(六)
七.用户登录与手机注册 7.1.drf的token (1)INSTALL_APP中添加 INSTALLED_APPS = ( ... 'rest_framework.authtoken' ) toke ...
- github git 在GitHub上创建项目并将本地项目push到网站上
众所周知,git是与svn类似的版本控制系统,git的去中心化.分布式等的优点,在不久将来用户量大有可能超过svn, 常见的代码托管网站有GitHub,coding.net, gitee.com 码云 ...