POJ2406-Power Strings-KMP循环节/哈希循环节
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.
题意:
求每个字符串最多是由多少个相同的子字符串重复连接而成的。
如:ababab 则最多有3个 ab 连接而成。
思路:两种方法求循环节的模板题。
哈希:
#include<stdio.h>
#include<string.h>
typedef unsigned long long ull;
const int N=1e6+;
char a[N];
ull p[N],sum[N],x=; void init()
{
p[]=;
for(int i=; i<N; i++)
p[i]=p[i-]*x;
} int main()
{
init();
while(~scanf("%s",a+))
{
if(a[]=='.')
break;
int len=strlen(a+);
sum[]=;
for(int i=;i<=len;i++)//主串哈希值
sum[i]=sum[i-]*x+a[i];
for(int i=; i<=len; i++)
{
if(len%i!=0)
continue;//说明不存在该循环节
int flag=;
for(int j=i; j<=len; j+=i)
{ //ababab
//i=2时 -> j=2、4、6
if((sum[j]-sum[j-i]*p[i])!=sum[i])
//(sum[2]-sum[2-2]*p[2])!=sum[2]
//(sum[4]-sum[4-2]*p[2])!=sum[2]
//(sum[6]-sum[6-2]*p[2])!=sum[2]
{
flag=;
break;
}
}
if(flag==)
{
printf("%d\n",len/i);
break;
}
}
}
return ;
}
KMP:
思想:
比如abcabcabcabc ,nextt[len]=9,len=12,所以len-next[len]=3肯定是len=12的因数,并且此时len-next[len]=3也肯定为最短循环节的长度,所以len/(len-next[len])=12/(12-9)=12/3=4,即循环节的个数,也就是出现过几次abc。
如果不能整除说明不存在循环节,直接输出1即可。
int w=len-nextt[len];
if(len%w==)
printf("%d\n",len/w);
else
printf("1\n");
详细代码如下:
#include<stdio.h>
#include<string.h>
const int N=1e6+; int nextt[N],len;
char a[N]; void getnext()
{
int i=,j=-;
nextt[]=-;
while(i<len)
{
if(j==-||a[i]==a[j])
{
nextt[++i]=++j;
}
else
j=nextt[j];
}
} int main()
{
while(~scanf("%s",a))
{
if(a[]=='.')
break;
len=strlen(a);
getnext();
len=strlen(a);
int w=len-nextt[len];
if(len%w==)
printf("%d\n",len/w);
else
printf("1\n");
}
return ;
}
POJ2406-Power Strings-KMP循环节/哈希循环节的更多相关文章
- POJ2406 Power Strings —— KMP or 后缀数组 最小循环节
题目链接:https://vjudge.net/problem/POJ-2406 Power Strings Time Limit: 3000MS Memory Limit: 65536K Tot ...
- poj2406 Power Strings (kmp 求最小循环字串)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 47748 Accepted: 19902 ...
- POJ2406 Power Strings(KMP)
Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 56162 Accepted: 23370 Description Giv ...
- poj 2406 Power Srings (kmp循环节) (经典)
<题目链接> 题目大意: 给出一个字符串,求其字串在该字符串中循环的最大周期. 解题分析: length=len-Next[len],len为该字符串的最小循环节,如果len%length ...
- POJ2406 Power Strings KMP算法
给你一个串s,如果能找到一个子串a,连接n次变成它,就把这个串称为power string,即a^n=s,求最大的n. 用KMP来想,如果存在的话,那么我每次f[i]的时候退的步数应该是一样多的 譬 ...
- POJ2406 Power Strings(KMP,后缀数组)
这题可以用后缀数组,KMP方法做 后缀数组做法开始想不出来,看的题解,方法是枚举串长len的约数k,看lcp(suffix(0), suffix(k))的长度是否为n- k ,若为真则len / k即 ...
- poj2406 Power Strings(kmp)
poj2406 Power Strings(kmp) 给出一个字符串,问这个字符串是一个字符串重复几次.要求最大化重复次数. 若当前字符串为S,用kmp匹配'\0'+S和S即可. #include & ...
- poj2406 Power Strings(kmp失配函数)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 39291 Accepted: 16315 Descr ...
- poj2406 Power Strings 【KMP】
Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc&quo ...
随机推荐
- hive建模方法
转自:https://www.jianshu.com/p/8378b80e4b21 概述数据仓库这个概念是由 Bill Inmon 所提出的,其功能是将组织通过联机事务处理(OLTP)所积累的大量的资 ...
- MAC OpenGL 环境搭建
MAC OpenGL 环境搭建 基础库介绍 先要安装两个库一个是GLEW(OpenGL Extension Wrangler Library),另外一个是GLFW(Graphics Library F ...
- VMware Workstation 无法打开内核设备:\\Global\\vmx86
解决方法:win10系统,打开“服务”后右击选择使用管理员打开.然后在一大串服务中找到vm开头的服务项,全部都启动.重新启动vm就ok了(vm需要以管理员身份打开).不用复杂的代码!!
- jQuery 事件委派
/******************************************************************/ $(function(){ //live()事件委派,后续添加 ...
- 1085 Perfect Sequence (25 分)
Given a sequence of positive integers and another positive integer p. The sequence is said to be a p ...
- 20140725 快速排序时间复杂度 sTL入门
1.快速排序的时间复杂度(平均时间复杂度为) 数组本身就有序时,效果很差为O(n^2) 2.STl入门 (1) C++内联函数(inline)和C中宏(#define)区别 内联函数有类型检查,宏定义 ...
- Java 序列化和反序列化(三)Serializable 源码分析 - 2
目录 Java 序列化和反序列化(三)Serializable 源码分析 - 2 1. ObjectStreamField 1.1 数据结构 1.2 构造函数 2. ObjectStreamClass ...
- 高效IO之File文件操作类的基础用法(二)
更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680 前言 众所周知Java提供File类,让我们对文件进行操作,下面就来简单整理了一 ...
- POJ 3259 Wormholes Bellman题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/.未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- 面向连接的echo服务编程实例
以下是echo_serv.c的源码,提供创建服务端,绑定套接字到本机IP的8080端口,当收到客户端发送的字符串就在屏幕上打印出来,并且把字符串发送给客户端 // echo_serv.c – gcc ...