- Power Strings (字符串哈希) (KMP)
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)的更多相关文章
- UVA 10298 Power Strings 字符串的幂(KMP,最小循环节)
题意: 定义a为一个字符串,a*a表示两个字符相连,即 an+1=a*an ,也就是出现循环了.给定一个字符串,若将其表示成an,问n最大为多少? 思路: 如果完全不循环,顶多就是类似于abc1这样, ...
- Codeforces 109D String Transformation 字符串 哈希 KMP
原文链接https://www.cnblogs.com/zhouzhendong/p/CF109D.html 题目传送门 - CF109D 题意 给定两个字符串 $a,b$ ,求一组 $i,j$ 使得 ...
- POJ1961 Period && POJ2604 Power Strings 字符串循环节
两道题都是求循环节的...但是一道是学哈希时做的,另一道是学$KMP$时做的 POJ2604 用的哈希...枚举长度的因数作为循环节的长度,然后暴力算出所有循环节位置的哈希值,看看是否相等. #inc ...
- POJ 2406 Power Strings (KMP)
Power Strings Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 29663Accepted: 12387 Descrip ...
- UVA - 10298 Power Strings (KMP求字符串循环节)
Description Problem D: Power Strings Given two strings a and b we define a*b to be their concatenati ...
- poj 2406:Power Strings(KMP算法,next[]数组的理解)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 30069 Accepted: 12553 D ...
- 【POJ2406】 Power Strings (KMP)
Power Strings Description Given two strings a and b we define a*b to be their concatenation. For exa ...
- Power Strings(kmp妙解)
Power Strings Time Limit : 6000/3000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tota ...
- Power Strings (poj 2406 KMP)
Language: Default Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 33205 ...
随机推荐
- 全面解读php-常量及数据类型
本文主要讲解字符串的定义方式,数据类型和常量的相关内容. 一.字符串的定义方式 1.字符串的定义方式除了单双引号外,还有一种叫 heredoc 和 newdoc 在我们需要定义很长一段儿字符串的时候 ...
- 第二章 SpringCloud之Eureka-Server服务发现组件
1.Eureka简介 文档:https://cloud.spring.io/spring-cloud-netflix/spring-cloud-netflix.html ############### ...
- Jmeter(五)检查点
录制的脚本回放成功了, 但是运行有可能出现失败的情况, 所以有必要让JMeter来帮我们验证测试结果的正确性. 在Jmeter中是用断言组件来实现此功能的. 首先, 在需要添加断言的请求后面, 添加响 ...
- ControlTemplate in WPF —— Shared in all file
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...
- python3.6+RF连接mysql
接口自动化中会遇到有操作数据库的动作 目录 1.安装第三方库 2.安装pymysql 3.数据库操作 1.安装第三方库 使用在线安装:pip install robotframework_databa ...
- k8s、CI/CD、pipline介绍
参照文档: https://blog.csdn.net/qq_35299863/article/details/84329798 https://github.com/xgh2016/k8s-CICD ...
- 配置linux系统时区---解决ntp同步完时间不准问题
ntp配置完成后时间仍然不准有下面两种情况: ntp服务端配置完ntp服务器后,查看时间和百度的时间不一样按照下面解决 ntp客户端同步完ntp服务器后,查看客户端的时间和百度不一致,按照下面解决 1 ...
- mysqli实现增删改查(转)
1.面向对象 在面向对象的方式中,mysqli被封装成一个类,它的构造方法如下: __construct ([ string $host [, string $username [, string $ ...
- vue-cli本地环境API代理设置和解决跨域
前言 我们在使用vue-cli启动项目的时候npm run dev便可以启动我们的项目了,通常我们的请求地址是以localhost:8080来请求接口数据的,localhost是没有办法设置cooki ...
- Unity中的动画系统和Timeline(3) 混合树和动画匹配
混合树 前面我们通过在Animation界面添加单独的动作来控制动画,这样做比较麻烦,每个单独的属性,比如站立,奔跑等,都需要单独的代码来控制.现在我们可以通过使用混合树,其基本思想是将相近的动画混合 ...