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. Java中的低级错误

    1.              不能用“==”比较两个字符串内容相等. 2.              对list做foreach循环时,循环代码中不能修改list的结构. 3.            ...

  2. pip安装selenium时,报错“You are using pip version 10.0.1, however version 18.0 is available.”的问题

    pip安装selenium,pip install selenium 类型这样错误 1  原因可能不是以管理员身份运行cmd安装selenium 2  解决方式 也是要管理员身份运行 重点在最后一句 ...

  3. JavaScript日常学习5

    JavaScript字符串属性和方法 eg :var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";      var sln = txt.length; ...

  4. Linux服务器重启后IP变掉的处理方式

    工作中有一台服务器为物理机,重启后IP就变掉了,影响到了使用,于是将服务器上的IP配置为静态方式,问题得以解决,具体如下: 1.登陆Linux服务器,cd /etc/sysconfig/network ...

  5. Android IdleHandler 原理浅析

    IdleHandler:空闲监听器(就像我没事做了,在群里发了个表情,这时候其他人就知道我很闲了) 在每次next获取消息进行处理时,发现没有可以处理的消息(队列空,只有延时消息并且没到时间,同步阻塞 ...

  6. Android 消息传递机制

    线程间消息传递机制 1.消息怎么发送的? 我们都知道当调用Handler发送消息的时候,不管是调用 sendMessage,sendEmptyMessage,sendMessageDelayed还是其 ...

  7. this引用逸出

    1.定义 public class UnsafeClass { public UnsafeClass(Button button) { button.addActionListener(new Act ...

  8. 【HANA系列】SAP HANA使用XS和HTTP创建proxy

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA使用XS和HTT ...

  9. Git提示要输入密码

    在服务器上Git pull或者push时,一直提示要输入密码,但这个密码又不是Gitlab账户的密码,经过同学指点,原来是项目主分支被保护起来了,处于protected的状态,因为目前只有我一个人 在 ...

  10. Python 类的私有属性与私有方法

    1.隐藏的使用场景 在Python类中,有些属性和方法只希望在对象的内部被使用,而不希望在外部被访问到, 2.定义方式, 在属性名或方法名前增加两个下划线,定义的就是私有属性或方法 #其实这仅仅这是一 ...