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 ...
随机推荐
- WarUtil
/** *包名:cn.yufu.utils *描述:package cn.yufu.utils; */ package cn.yufu.utils; import java.io.File; impo ...
- 线性基思想+贪心——cf1249C
/*1+3+9+...+3^n<3^(n+1),按这个思路贪心一下就好*/#include<bits/stdc++.h> using namespace std; #define l ...
- 收集python2代码转python3遇到的问题
在程序中做python版本判断 sys.version_info # sys.version_info(major=2, minor=7, micro=16, releaselevel='final' ...
- Awesome Adb——一份超全超详细的 ADB 用法大全
https://github.com/mzlogin/awesome-adb https://www.cnblogs.com/bravesnail/articles/5850335.html ...
- 1、Locust压力测试环境搭建
环境准备:阿里云服务器一台.python2.7.pip Locust 介绍 Locust 是一个开源负载测试工具.使用 Python 代码定义用户行为,也可以仿真百万个用户. Locust 简单易用, ...
- 【Linux】- CentOS安装docker及docker-compose
1.安装docker,命令如下: -- 把yum包更新到最新 yum update -- 安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是devicema ...
- C# WinfForm 控件之dev报表 XtraReport (一) 初了解
这个控件其实用法和fast也差不了太多但如果没接触过 真有种老虎吃天的感觉 1.这里先不说那些高深的先说最基本的 在窗体中显示一个设计好的 模版 1.1一般设计和这个程序是分着的为了方便我就先把他们合 ...
- idea springboot 打包 war
1.pom文件中将项目改为 war
- Solr6.6环境安装及core的创建(win7环境)
1.下载solr6.6 并解压 地址: http://www.apache.org/dyn/closer.lua/lucene/solr/6.6.0 2.安装JDK1.8 地址: http://www ...
- openssl编译方法
受不了了,终于编译成功了openssl,写一下编译方法吧 准备: 0:要编译openssl,必不可少的是代码,去下载 https://www.openssl.org/source/ 1:要有一个VS系 ...