点击打开链接

Power Strings
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 27368   Accepted: 11454

Description

Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "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

abcd
aaaa
ababab
.

Sample Output

1
4
3

Hint

This problem has huge input, use scanf instead of cin to avoid time limit exceed.

kmp算法里的make next函数就可以了,不过我今天还是没搞明白这个算法的原理,只能说暂时能写对这个算法

#include<stdio.h>
#include<string.h>
char str[1000];
int next[1000];
void make_next()
{
int i = 1, j = 0;
next[0] = -1;
while(str[i])
{
if(j == -1 || str[j] == str[i])
next[++i] = ++j;
else
j = next[j];
}
}
int main()
{
while(scanf("%s", str), str[0] + str[1] != '.')
{
make_next();
int i = strlen(str);
int len = i - next[i];
if(i % len == 0)
printf("%d\n",i / len);
else printf("1\n");
}
return 0;
}

poj 2406 Power Strings kmp算法的更多相关文章

  1. POJ 2406 Power Strings KMP算法之next数组的应用

    题意:给一个字符串,求该串最多由多少个相同的子串相接而成. 思路:只要做过poj 1961之后,这道题就很简单了.poj 1961 详细题解传送门. 假设字符串的长度为len,如果 len % (le ...

  2. POJ 2406 Power Strings (KMP)

    Power Strings Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 29663Accepted: 12387 Descrip ...

  3. poj 2406 Power Strings (kmp 中 next 数组的应用||后缀数组)

    http://poj.org/problem?id=2406 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submiss ...

  4. poj 2406 Power Strings(KMP变形)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 28102   Accepted: 11755 D ...

  5. POJ 2406 - Power Strings - [KMP求最小循环节]

    题目链接:http://poj.org/problem?id=2406 Time Limit: 3000MS Memory Limit: 65536K Description Given two st ...

  6. POJ 2406 Power Strings KMP求周期

    传送门 http://poj.org/problem?id=2406 题目就是求循环了几次. 记得如果每循环输出为1.... #include<cstdio> #include<cs ...

  7. POJ 2406 Power Strings KMP运用题解

    本题是计算一个字符串能完整分成多少一模一样的子字符串. 原来是使用KMP的next数组计算出来的,一直都认为是能够利用next数组的.可是自己想了非常久没能这么简洁地总结出来,也仅仅能查查他人代码才恍 ...

  8. poj 2406 Power Strings KMP匹配

    对于数组s[0~n-1],计算next[0~n](多计算一位). 考虑next[n],如果t=n-next[n],如果n%t==0,则t就是问题的解,否则解为1. 这样考虑: 比方字符串"a ...

  9. KMP POJ 2406 Power Strings

    题目传送门 /* 题意:一个串有字串重复n次产生,求最大的n KMP:nex[]的性质应用,感觉对nex加深了理解 */ /************************************** ...

随机推荐

  1. Knockout.js, Asp.Net MVC and Bootstrap 前端设计

    原文地址:http://ddmvc4.codeplex.com/ 原文名称:Design and Develop a website using ASP.NET MVC 4, EF, Knockout ...

  2. ORA-27086: unable to lock file - already in use

    问题现象: SQL> startup ORACLE instance started. Total System Global Area 1854021632 bytes Fixed Size  ...

  3. CGI相关概念

    common gateway interface 通用网关接口 可以让客户端从浏览器向执行在服务器上的程序请求数据.CGI描述了客户端和服务器程序之间传输数据的一种标准. 编程语言perl是一种被广泛 ...

  4. SQL 查询本月无数据用上个月的数据问题

    SQL 查询本月无数据用上个月的数据 前言 因为标题有长度限制,先简要说明一下应用场景.比如我们要查一段时间范围内(2013-08-01至2013-12-31)每个月每个运营商的用户总量,每个运营商用 ...

  5. 【phantomjs】使用phantomjs生成highChart的图片(待完善)

    阅读目录 //center }, subtitle: { text: 'Source: WorldClimate.com', x: -20 }, xAxis: { categories: ['Jan' ...

  6. oracle学习笔记(二)设置归档模式

    设置归档模式(mount状态) ALTER database ARCHIVELOG; //关闭数据库 shutdown immediate //启动数据库到mount状态 startup mount ...

  7. haproxy实现负载均衡

    一.安装tar zxvf haproxy-1.4.22.tar.gzcd haproxy-1.4.22make TARGET=linux26 PREFIX=/usr/local/haproxy ins ...

  8. PHP之单例模式的实现

    单例模式: 单例模式又称职责模式:简单的说,一个对象(在学习设计模式之前,需要比较了解面向对象思想)只负责一个特定的任务: 单例类: 1.构造函数需要标记为private(访问控制:防止外部代码使用n ...

  9. 黄聪:Mysql5.6缓存命中率

    MySQL缓存命中率,网上说法不一,下面我说下我的看法,大家轻拍: 总的select查询数等于com_select(没命中) + qcache_hits(命中) + 解析错误的查询. 再来看看Com_ ...

  10. 黄聪:wordpress如何使用wp_rewrite实现自定义伪静态,非301重定向。

    今天,想通过wordpress实现 http://hcsem.com/a?h-1 伪静态为 http://hcsem.com/a-1.html 找了很多资料,终于搞定. 只需要在functions.p ...