Power Strings (poj 2406 KMP)

简介:
Language: Default
Power Strings
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 33205   Accepted: 13804

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.

Source

题意:给一个字符串S长度不超过10^6,求最大的n使得S由n个同样的字符串a连接而成。如:"ababab"则由n=3个"ab"连接而成。"aaaa"由n=4个"a"连接而成,"abcd"则由n=1个"abcd"连接而成。

定理:如果S的长度为len。则S存在循环子串,当且仅当,len能够被len - next[len]整除,最短循环子串为S[len - next[len]]

思路:利用KMP算法,求字符串的特征向量next,若len能够被len - next[len]整除。则最大循环次数n为len/(len - next[len]),否则为1。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;

int N;
int nextval[1000010];
char str[1000010];

int get_nextval()
{
    int i=0;
    int len=strlen(str);
    int j=-1;
    nextval[i]=-1;
    while (i<len)
    {
        if (j==-1||str[i]==str[j])
        {
            i++;
            j++;
            nextval[i]=j;
        }
        else
            j=nextval[j];
    }
    if ((len)%(len-nextval[len])==0)
        return len/(len-nextval[len]);
    else
        return 1;
}

int main()
{
    while (scanf("%s",str))
    {
        if (str[0]=='.')
            return 0;
        printf("%d\n",get_nextval());
    }
    return 0;
}


版权声明:本文博客原创文章,博客,未经同意,不得转载。





本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4636202.html,如需转载请自行联系原作者


相关文章
|
机器学习/深度学习 自然语言处理 PyTorch
Dropout和R-Dropout的使用技巧
【8月更文挑战第4天】Dropout及其扩展R-Dropout在机器学习中的应用,包括Dropout解决过拟合问题的方法、最佳实践技巧以及R-Dropout如何通过两次前向传播和损失函数正则化来提高模型的泛化能力。
282 0
|
数据可视化 前端开发 JavaScript
react+datav+echarts实现可视化数据大屏
最近有点闲,就学习了下react,没想到就把react学完了,觉得还不错,就打算出一把react+datav的简易版可视化数据大屏供大家做个参考
1340 2
react+datav+echarts实现可视化数据大屏
|
缓存 编译器 程序员
C/C++编译器链接优化技术:链接优化是在编译器和链接器之间进行的优化
C/C++编译器链接优化技术:链接优化是在编译器和链接器之间进行的优化
617 0
|
C++ 容器
STL中会用到的函数
这段代码示例展示了C++中几种常用容器的使用,包括`vector`、`list`、`map`、`queue`、`deque`和`stack`。它涵盖了初始化、操作方法如添加、删除元素、排序、查找以及容器属性的查询等。同时,还提到了`algorithm`库中的`erase`、`sort`和边界查找函数。
96 0
|
机器学习/深度学习 分布式计算 并行计算
当 Mars 遇上 RAPIDS:用 GPU 以并行的方式加速数据科学
在数据科学世界,Python 是一个不可忽视的存在,且有愈演愈烈之势。而其中主要的使用工具,包括 Numpy、Pandas 和 Scikit-learn 等。 Mars 在 MaxCompute 团队内部诞生,它的主要目标就是让 Numpy、pandas 和 scikit-learn 等数据科学的库能够并行和分布式执行,支持通过 RAPIDS 平台用 GPU 加速数据科学。
2343 0
当 Mars 遇上 RAPIDS:用 GPU 以并行的方式加速数据科学
|
算法 PHP
m根据给定系统传递函数自动绘制系统结构图matlab仿真,包括直接型,级联型以及并联型
m根据给定系统传递函数自动绘制系统结构图matlab仿真,包括直接型,级联型以及并联型
582 0
|
Rust JavaScript 前端开发
Rspack 学习了解
Rspack 学习了解
334 0
|
安全 NoSQL Java
基于springboot+Redis的前后端分离项目(三)-【黑马点评】
当用户抢购时,就会生成订单并保存到tb_voucher_order这张表中,而订单表如果使用数据库自增ID就存在一些问题:id的规律性太明显,受单表数据量的限制。场景分析:如果我们的id具有太明显的规则,用户或者说商业对手很容易猜测出来我们的一些敏感信息,比如商城在一天时间内,卖出了多少单,这明显不合适。场景分析二:随着我们商城规模越来越大,mysql的单表的容量不宜超过500W,数据量过大之后,我们要进行拆库拆表,但拆分表了之后,他们从逻辑上讲他们是同一张表,所以他们的id是不能一样的, 于是乎我们需要保证id的唯一性。
BXA
|
C++
C++使用中需要避免的10个常见错误
C++使用中需要避免的10个常见错误
BXA
469 0
|
机器学习/深度学习 存储 Java
【Java基础】--总结【包含使用范例】
【Java基础】--总结【包含使用范例】
89 0