Power Strings

Description

- Given two strings a and b we define ab to be their concatenation. For example, if a = "abc" and b = "def" then ab = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).

Input

- Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.

Output

- For each s you should print the largest n such that s = a^n for some string a.

Sample Input 1

- abcd
aaaa
ababab
.

Sample Output 1

- 1
4
3

思路1

暴力

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std; const int Max=1000001;
string a;
int x,n,m;
int p[Max]; void prime(int n)
{
int k=(int)sqrt(n*1.0);
p[1]=1;
for(int i=2; i<=k; i++)
if(n%i==0)
p[++m]=i;
int end=m;
if(k*k==n) end--;
for(int i=end; i>=1; i--)
p[++m]=n/p[i];
} bool ok(int x)
{
string s=a.substr(0,x);
for(int i=0; i<a.size(); i+=x)
if(s!=a.substr(i,x)) return false;
return true;
} int main()
{
while(getline(cin,a))
{
x=1,n=a.size();m=1;
prime(n);
while(x<=m)
{
if(ok(p[x]))
{cout<<a.size()/p[x]<<endl;break;}
x++;
}
}
return 0;
}

思路2

利用\(KMP\)的\(next[]\)数组

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std; const int Max=1000001;
string a;
int N,M,ans,nx[Max]; void makenx(int M)
{
memset(nx,0,sizeof(nx));
int i=0,j=-1;
nx[i]=j;
while(i<M)
{
if(j==-1||a[i]==a[j]) i++,j++,nx[i]=j;
else j=nx[j];
}
} int main()
{
bool fl;
while(getline(cin,a)&&a[0]!='.')
{
fl=true;
M=a.size();makenx(M);
if(M%(M-nx[M])==0) cout<<M/(M-nx[M])<<endl;
else cout<<1<<endl;
}
return 0;
}

Power Strings[poj2406]题解的更多相关文章

  1. Power Strings poj2406(神代码)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 29402   Accepted: 12296 D ...

  2. Power Strings POJ2406 KMP 求最小循环节

    相比一般KMP,构建next数组需要多循环一次,因为next[j]代表前j-1个字符的最长相同前缀后缀,比如字符串为aab aab aab共9个字符,则next[10]等于前9个字符中最长相同前缀后缀 ...

  3. 【POJ2406】 Power Strings (KMP)

    Power Strings Description Given two strings a and b we define a*b to be their concatenation. For exa ...

  4. poj2406 Power Strings(kmp)

    poj2406 Power Strings(kmp) 给出一个字符串,问这个字符串是一个字符串重复几次.要求最大化重复次数. 若当前字符串为S,用kmp匹配'\0'+S和S即可. #include & ...

  5. POJ2406 Power Strings —— KMP or 后缀数组 最小循环节

    题目链接:https://vjudge.net/problem/POJ-2406 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Tot ...

  6. poj2406 Power Strings (kmp 求最小循环字串)

    Power Strings   Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 47748   Accepted: 19902 ...

  7. poj2406 Power Strings(kmp失配函数)

    Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 39291 Accepted: 16315 Descr ...

  8. Power Strings(kmp妙解)

    Power Strings Time Limit : 6000/3000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tota ...

  9. 「UVA10298」 Power Strings(KMP

    题目描述 PDF 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 复制 abcd aaaa ababab . 输出样例#1: 复制 1 4 3 题解 Luogu的题解 这里是对目前 ...

随机推荐

  1. 如何在GitHub预览html

    1.在GitHub中找到需要预览的html文件,点击settings 2.找到GitHub Pages,将其中的source改为master branch,此时出现了下图中的红色链接,打开一个新的网页 ...

  2. Python json格式处理

    Python json格式处理 首先放一段代码 import requests import jsonpath import json f=open('ip.txt','r',encoding='ut ...

  3. Gong服务实现平滑重启分析

    平滑重启是指能让我们的程序在重启的过程不中断服务,新老进程无缝衔接,实现零停机时间(Zero-Downtime)部署: 平滑重启是建立在优雅退出的基础之上的,之前一篇文章介绍了相关实现:Golang中 ...

  4. cookie的设置与取值

    设置cookie function cookie(key, value, options) { let days let time let result // A key and value were ...

  5. count(1)比count(*)效率高?

    SELECT COUNT(*) FROM table_name是个再常见不过的统计需求了. 本文带你了解下Mysql的COUNT函数. 一.COUNT函数 关于COUNT函数,在MySQL官网中有详细 ...

  6. MRAM技术进入汽车应用

    在整个地址空间范围内读写各种类型的数据.通常MRAM的操作和时序类似于32位微控制器的规范和时序.与DLFASH相比,当今的非易失性存储器可以接受MRAM设备的性能和吞吐量. 与当今的DFLASH相比 ...

  7. FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecate;的解决办法

    踩坑场景 报错FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecate; 解决办法 1.升级numpy ...

  8. 如何将旧Mac的数据迁移到新的MacBook Pro?

    最新版的MacBook Pro已经上市,具有超凡魅力的Touch Bar开创了一个新时代.苗条的设计和华丽的显示效果也起到了推动运动的作用……!将数据从旧Mac传输到新Mac不再是一件漫长的事.您只需 ...

  9. Beego 过滤器

    过滤器 beego 支持自定义过滤中间件,例如安全验证,强制跳转等. 过滤器函数如下所示: beego.InsertFilter(pattern string, position int, filte ...

  10. JBPM4 学习笔记 转

    关于JBPM工作流 2.        JBPM jBPM,全称是Java Business Process Management,是一种基于J2EE的轻量级工作流管理系统.JBPM使用Hiberna ...