点击打开链接

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. 怎么优化JAVA程序的执行效率和性能?

    现在java程序已经够快的了,不过有时写出了的程序效率就不怎么样,很多细节值得我们注意,比如使用StringBuffer或者StringBuilder来拼接或者操作字符串就比直接使用String效率高 ...

  2. LintCode "Delete Digits"

    Greedy: remove earliest down-edge: like "54", "97". class Solution { public: /** ...

  3. Maven修改镜像仓库地址

    修改maven根目录下的conf文件夹中的setting.xml文件,如果你修改了默认仓库的存储位置,即.m2文件夹下没有本地仓库,但是有个setting.xml文件,那就修改这个文件就可以. 具体内 ...

  4. android学习笔记30——AndroidMainfest.xml

    Manifest.xml文件的职责:指定APP的包名.声明四大组件, 以及启动方式.指定APP运行的进程名称.指定APP权限.指定最小API版本.指定需要连接的库. Manifest.xml的格式:& ...

  5. 【freemaker】之整合springMVC

    pom.xml文件 <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncod ...

  6. 文件压缩与挤压ZIP

    /// <summary> /// Zip压缩与解压缩 /// </summary> public class ZipHelper { /// <summary> ...

  7. System.Data.SqlClient.SqlException.Number的所有错误值列表

    在系统数据库(master或msdb或model)的架构(sys)的视图(messages)中: SELECT [message_id]      ,[language_id]      ,[seve ...

  8. SqlServer——阻止保存要求重新创建表的更改

    场景: 修改已有数据的列宽时,提示“阻止保存要求重新创建表的更改”. 解决: 工具-〉选项-〉左侧有个 设计器-〉表设计器和数据库设计器 -> 阻止保存要求重新创建表的更改(右侧) 把钩去掉即可 ...

  9. [系统开发] FileMaker进销存系统

    一.简介 这是我用 FileMaker 编写的进销存系统: FileMaker 是一种在欧美流行的桌面型数据库:它使用非常方便,功能也很强大,用户可以在它上面开发自己的系统: 开发时间:2008年 二 ...

  10. PHP 正则表达式常用函数使用小结

    在PHP中有两套正则表达式函数库.一套是由PCRE(Perl Compatible Regular Expression)库提供的.PCRE库使用和Perl相同的语法规则实现了正则表达式的模式匹配,其 ...