https://www.cnblogs.com/widsom/p/8058358.htm  (详细解释)

//#include<bits/stdc++.h>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#define ull unsigned long long
using namespace std;
const int maxn=1e6+;
char a[maxn];
ull b[maxn];
ull c[maxn];
int main()
{
b[]=; ull d=;
for(int i=;i<=maxn;i++) b[i]=b[i-]*d;
while()
{
scanf("%s",a+); int n=strlen(a+);
if(n== && a[]=='.') break;
for(int i=;i<=n;i++) c[i]=c[i-]*d+(a[i]-'a'+);
vector<int> vs; for(int i=;i<=n;i++) { if(n%i==) vs.push_back(i); }
int ans=;
for(int i=;i<vs.size();i++)
{
int flag=;
for(int j=*vs[i];j<=n;j+=vs[i])
{
ull x=c[j]-c[j-vs[i]]*b[vs[i]];
if(x!=c[vs[i]]) {flag=; }
}
if(flag) {ans=n/vs[i]; break; }
}
printf("%d\n",ans);
}
}
//#include<bits/stdc++.h>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#define ull unsigned long long
using namespace std;
const int maxn=1e6+;
char ss[maxn];
int nt[maxn];
int KMP()
{
int l=strlen(ss);
nt[]=-;
for (int i = ; i < l; ++i) {
int j = nt[i-];
while(ss[j+] != ss[i] && j >= ) j = nt[j];
if(ss[j+] == ss[i]) nt[i] = j+;
else nt[i] = -;
}
for(int i=;i<l;i++)
{
cout<<nt[i]<<endl;
}
int k=l-(nt[l-]+);
if(l%k==) return l/k;
else return ;
}
int main()
{ while()
{
scanf("%s",ss); int n=strlen(ss);
if(n== &&ss[]=='.') break;
printf("%d\n",KMP());
}
}

KMP

//#include<bits/stdc++.h>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#define ull unsigned long long
using namespace std;
const int maxn=1e6+;
char ss[maxn];
int nt[maxn];
int KMP()
{
int l=strlen(ss); // ss
//int t=strlen(tt);
nt[]=-;
int num=;
for(int i=,j=-;i<l;)
{
if(j==-||ss[i]==ss[j])
{
nt[i]=j; // next[1]=
i++; j++; }
else
j=nt[j];
}
int k=l-(nt[l-]+);
if(l%k==) return l/k;
else return ;
}
int main()
{ while()
{
scanf("%s",ss); int n=strlen(ss);
if(n== &&ss[]=='.') break;
printf("%d\n",KMP());
}
}

KMP

桶排序

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define rank rk
using namespace std; typedef long long LL; const int maxn = , M = ;
// sa[1-n] -> 0->n-1;
int sa[maxn], rank[maxn], height[maxn]; int wa[maxn], wb[maxn], wv[maxn], cnt[maxn];
void SA(int *r, int n, int m) {
int *x = wa, *y = wb;
for(int i = ; i < m; i++) cnt[i] = ;
for(int i = ; i < n; i++) cnt[x[i] = r[i]]++;
for(int i = ; i < m; i++) cnt[i] += cnt[i - ];
for(int i = n - ; i >= ; i--) sa[--cnt[x[i]]] = i;
for(int j = ; j < n; j <<= ) {
int p = ;
for(int i = n - j; i < n; i++) y[p++] = i;
for(int i = ; i < n; i++) if(sa[i] >= j) y[p++] = sa[i] - j;
for(int i = ; i < n; i++) wv[i] = x[y[i]];
for(int i = ; i < m; i++) cnt[i] = ;
for(int i = ; i < n; i++) cnt[wv[i]]++;
for(int i = ; i < m; i++) cnt[i] += cnt[i - ];
for(int i = n - ; i >= ; i--) sa[--cnt[wv[i]]] = y[i];
swap(x, y);
p = ; x[sa[]] = ;
for(int i = ; i < n; i++)
x[sa[i]] = y[sa[i - ]] == y[sa[i]] && y[sa[i - ] + j] == y[sa[i] + j] ? p - : p++;
if(p >= n) break;
m = p;
}
} void calcHeight(int *r, int n) {
int i, j, k;
for(i = j = k = ; i < n; height[rank[i++]] = k)
for(k ? k-- : , j = sa[rank[i] - ]; r[i + k] == r[j + k]; k++);
} int n, s[maxn];
char str[maxn];
int lg[maxn];
int main() {
for(int i=;i<maxn;i++){lg[i]=lg[i>>]+;}
while()
{
scanf("%s", str); n = strlen(str);
if(n== && str[]=='.') break;
for(int i = ; i < n; i++) s[i] = str[i]; s[n] = ;
SA(s, n + , M);
for(int i = ; i <= n; i++) rank[sa[i]] = i;
calcHeight(s, n);
int ans = ;
for (int i = ; i <= n; ++i) {
if(n%i == ) {
if(rank[]-rank[i] == && height[rank[]] == n-i) {
ans = n/i;
break;
}
}
}
printf("%d\n",ans);
}
return ;
}

会T

DC3

- Power Strings (字符串哈希) (KMP)的更多相关文章

  1. UVA 10298 Power Strings 字符串的幂(KMP,最小循环节)

    题意: 定义a为一个字符串,a*a表示两个字符相连,即 an+1=a*an ,也就是出现循环了.给定一个字符串,若将其表示成an,问n最大为多少? 思路: 如果完全不循环,顶多就是类似于abc1这样, ...

  2. Codeforces 109D String Transformation 字符串 哈希 KMP

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF109D.html 题目传送门 - CF109D 题意 给定两个字符串 $a,b$ ,求一组 $i,j$ 使得 ...

  3. POJ1961 Period && POJ2604 Power Strings 字符串循环节

    两道题都是求循环节的...但是一道是学哈希时做的,另一道是学$KMP$时做的 POJ2604 用的哈希...枚举长度的因数作为循环节的长度,然后暴力算出所有循环节位置的哈希值,看看是否相等. #inc ...

  4. POJ 2406 Power Strings (KMP)

    Power Strings Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 29663Accepted: 12387 Descrip ...

  5. UVA - 10298 Power Strings (KMP求字符串循环节)

    Description Problem D: Power Strings Given two strings a and b we define a*b to be their concatenati ...

  6. poj 2406:Power Strings(KMP算法,next[]数组的理解)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30069   Accepted: 12553 D ...

  7. 【POJ2406】 Power Strings (KMP)

    Power Strings Description Given two strings a and b we define a*b to be their concatenation. For exa ...

  8. Power Strings(kmp妙解)

    Power Strings Time Limit : 6000/3000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tota ...

  9. Power Strings (poj 2406 KMP)

    Language: Default Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33205   ...

随机推荐

  1. Serializable 和 Parcelable 的区别?

    1.在使用内存的时候,Parcelable 类比 Serializable 性能高,所以推荐使用 Parcelable 类.2.Serializable 在序列化的时候会产生大量的临时变量,从而引起频 ...

  2. KahnProcessNetwork的Python实现

    用Pytho实现了一个Kahn Process Network: 思路: 用Python的list模拟queue. 每个channel一个queue 用一个list (fgLog)来记录所有push到 ...

  3. python3.6+RF连接mysql

    接口自动化中会遇到有操作数据库的动作 目录 1.安装第三方库 2.安装pymysql 3.数据库操作 1.安装第三方库 使用在线安装:pip install robotframework_databa ...

  4. zabbix服务器性能监控工具的安装一

    Linux下安装zabbix需要有LNMP运行环境,本篇记录安装LNMP的过程 目录 1.准备工作 2.安装mysql 3.安装nginx 4.安装php 1.准备工作 1.1准备一台centos6. ...

  5. MySQL 常用工具sysbench/fio/tpcc等测试

    为什么要压力测试采购新设备,评估新设备性能开发新项目,评估数据库容量新系统上线前,预估/模拟数据库负载更换数据库版本,评估性能变化 关注指标  CPU %wait,%user,%sys 内存 只内存读 ...

  6. python--008文件处理

    一.文件操作 1.打开文件,获得文件句柄,并将句柄赋值给一个变量 2.通过句柄对文件操作 3.关闭文件 f=open('sg',encoding='utf-8') da=f.read() print( ...

  7. javase程序设计上机作业1

    package javaxgp.teacher.test; import java.math.BigInteger; import java.util.Scanner; import java.uti ...

  8. EINT DINT ERTM DRTM EALLOW EDIS ESTOP0的理解

    本文参考以下资料整理 https://wenku.baidu.com/view/6b0d6906cf84b9d528ea7a66.html http://pangqicheng123.blog.163 ...

  9. 【数字图像处理】Bilateral Filters

    [数字图像处理]Bilateral Filters https://www.yuque.com/lart/idh721/bf 简单介绍 双边滤波是一种非线性的可以模糊图像并且能保留一定的边缘信息的技术 ...

  10. C#后台正则表达式

    static Regex RegPhone = new Regex(@"^((1[34578][0-9]{1}))\d{8}");        static Regex RegN ...