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. Windows下无法颜色转义

    只需在最前面添加 import os os.system("") 参考文献:https://blog.csdn.net/ytzlln/article/details/8194524 ...

  2. 访问控制列表 ACL

    访问控制列表(Access Control List,ACL) 是路由器和交换机接口的指令列表,用来控制端口进出的数据包.其目的是为了对某种访问进行控制. 作用 ACL可以限制网络流量.提高网络性能. ...

  3. MacBook通过SSH远程访问Parallel中的Ubuntu简明教程

    作为一个前端,后端也需要了解,最终选择PHP入手学习,本来想选择Python,思前想后还是PHP作为Web开发比较合适,环境最终选择Ubuntu开发,由于是第一次,遇到不少坑,经过不懈的努力不断Goo ...

  4. 2020/1/4 H5&&C3笔记

    1. 类名不能由数字开头 2.float 是float 属性定义元素在哪个方向浮动.有left / right / none / inherit四个 参考https://www.w3school.co ...

  5. win10上使用linux命令

    (1)可以用windows自带的powershell,但是 ll,vim等命令不能使用 (2)Windows更新==>针对开发人员==>开启开发人员模式,然后在控制面板==>程序与功 ...

  6. 【STM32H7教程】第61章 STM32H7的MDMA基础知识和HAL库API

    完整教程下载地址:http://www.armbbs.cn/forum.php?mod=viewthread&tid=86980 第61章       STM32H7的MDMA基础知识和HAL ...

  7. PPT导出为图片

    使用Aspose组件导出 Aspose有Aspose.Slides.dll,可以无需安装office,进行读写PPT文件. Aspose可能通过Aspose.Slides.NET安装 简单的导出图片d ...

  8. VSTO开发指南(VB2013版) 第三章 Excel编程

    通过前两章的内容,有了一定的基础,但进入第三章,实例的步骤非常多,并且随着VS版本的升级,部分功能菜单界面发生了很大变化,所以,第三章的案例我将逐步编写! 实例3.1的目标就是给Excel写一个加载宏 ...

  9. django 定时任务 django-crontab 的使用

    成功例子如下图: 1.前言 在做 django 开发需求时,多多少少都会遇到需要定时任务的功能,比如定时执行任务,检查订单之类的.可能是一段时间,比如每隔 10分钟执行一次,也可能是定点时间,比如 1 ...

  10. 07.JS对象-2

    前言: 学习一门编程语言的基本步骤(01)了解背景知识(02)搭建开发环境(03)语法规范(04)常量和变量(05)数据类型(06)数据类型转换(07)运算符(08)逻辑结构(09)函数(10)对象1 ...