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 ...
随机推荐
- [NOIP]模拟17 题解
A.入阵曲 部分分很肥,正解写得常数稍大就会和暴力一个分,考试的时候写什么自己考虑.(滑稽 部分分的循环边界手抖写错了-25 (原本暴力分中的10分都没了啊啊啊) 没写挂的话应该有75,其实就是二维前 ...
- pandas中读取文件报错
import pandas as pd fileName = "路径中带有中文/xxx.csv" tf_train = pd.read_csv(fileName) 会提示报错 OS ...
- WebKit资源
WebKit 资料搜集 1 what is webkit? WebKit 是一个开源浏览器网页排版引擎,与之相应的引擎有Gecko(Mozilla,Firefox 等使用的排版引擎)和Trident( ...
- 1.6 flux介绍
这一节将介绍 React 的核心应用架构模式 Flux,包括内容: Flux 介绍 MVC 架构之痛 Flux 的理解 Flux 相关库和工具介绍 Flux 与 React 实例 最后我们将会把之前的 ...
- C# 简单的往txt中写日志,调试时很有用
原文 http://blog.csdn.net/hejialin666/article/details/6106648 有些程序在调试时很难抓住断点(如服务程序),有些程序需要循环无数次,要看每一次或 ...
- 021_IO流
IO流 I——input 输入 O——Output 输出 文件 File 所有文件类的父类 创建方式 File file = new File("文件路径"); 文件对象常用的方法 ...
- html5语义化标签大全
常见的语义化标签有 <article>.<section>.<nav>.<aside>.<header>.<footer> 详细 ...
- Front Page
General Team FST stay night from ShanDong University 19 - 20 CCPC QinHuangDao Gold (4 th) IUPC YinCh ...
- 转载:mysql sql_safe_updates 分析
今天看到一个很实用的功能,mysql_safe_updates. 只是对功能做了转载,具体原理可以看一下 delete from table t where true ; update t set c ...
- Vue之自建管理后台(二)Vue端设计
我们先设计Vue的文件夹分布. 在此之前,我们先了解下初始化创建的Vue的文件夹 https://www.cnblogs.com/luoxuemei/p/9812151.html (我引用了这哥们写的 ...