后缀数组+莫比乌斯函数
#include <stdio.h>
#include <string.h>
#include<algorithm>
using namespace std;
const int N = ; const int MAXN = N; //数列的最大值和个数上界 struct SuffixArray{
int wa[MAXN]; //用来进行基数排序或临时变量
int wb[MAXN]; //用来进行基数排序或临时变量
int wv[MAXN]; //用来进行基数排序或临时变量
int ws[MAXN]; //用来进行基数排序或临时变量 int sa[MAXN]; //sa[i]代表排名为i的后缀在原数列起始下标(数列的下标从0开始),sa[0]肯定等于n,因为标兵为最小的。
int rank[MAXN]; //rank[i]代表suffix[i]的排名,rank[n]肯定等于0,理由同上。
int height[MAXN]; //height[i]代表排名为i - 1的后缀 和排名为i的后缀 的最长公共连续子序列 的长度。
int r[MAXN]; //r[]存放原数列下标从0到n,r[n]为a标兵,是r[]里面最小的.
int n; //数列的元素个数,不包括标兵
int m; //存放最大值,r[]数组的数都要小于m,用来进行基数排序 void input(int *val, int len, int Max){//Max要大于r[0..len - 1],因为内部采用了基数排序
for (int i = ;i < len;i++)
r[i] = val[i];
r[len] = ; //最小值,起标兵作用
n = len;
m = Max;
calSa();
calHeight();
} int cmp(int *r, int a, int b, int l){
return (r[a] == r[b] && r[a + l] == r[b + l]);
} void calSa(){ //求sa数组
int i, j, p, *x = wa, *y = wb, *t;
for (i = ;i < m;i++) ws[i] = ;
for (i = ;i < n + ;i++) ws[x[i] = r[i]]++;
for (i = ;i < m;i++) ws[i] += ws[i - ];
for (i = n;i >= ;i--) sa[--ws[x[i]]] = i;
for (j = , p = ;p < n + ;j *= , m = p){
for (p = , i = n - j + ;i < n + ;i++) y[p++] = i;
for (i = ;i < n + ;i++) if (sa[i] >= j) y[p++] = sa[i] - j;
for (i = ;i < n + ;i++) wv[i] = x[y[i]];
for (i = ;i < m;i++) ws[i] = ;
for (i = ;i < n + ;i++) ws[wv[i]]++;
for (i = ;i < m;i++) ws[i] += ws[i - ];
for (i = n;i >= ;i--) sa[--ws[wv[i]]] = y[i];
for (t = x, x = y, y = t, p = , x[sa[]] = , i = ; i < n + ;i++)
x[sa[i]] = cmp(y, sa[i - ], sa[i], j) ? p - : p++;
}
} void calHeight(){ //求rank和height数组
int i, j, k = ;
for (i = ;i <= n;i++) rank[sa[i]] = i;
for (i = ;i < n;height[rank[i++]] = k)
for (k?k--:, j = sa[rank[i]- ];r[i + k] == r[j + k];k++);
}
//-求任意两后缀最长公共前缀问题,如果没用到可以去掉----------------------
int Log[MAXN];
int best[][MAXN];
void initRMQ() { //初始化RMQ 标准RMQ 预处理O(nlgn)
Log[] = -;
for(int i = ;i <= MAXN;i++){
Log[i]=(i & (i - ))?Log[i - ] : Log[i - ] + ;
}
for(int i = ; i <= n ; i ++) best[][i] = height[i];
for(int i = ; i <= Log[n] ; i ++) {
int limit = n - (<<i) + ;
for(int j = ; j <= limit ; j ++) {
best[i][j] = (best[i-][j] > best[i-][j+(<<i>>)]) ? best[i-][j+(<<i>>)] : best[i-][j];
}
}
}
int lcp(int a,int b) { //询问suffix[a]于suffix[b]的最长公共前缀(标准RMQ 询问O(1)) 使用这个函数之前要先后调用initRMQ()和input()
a = rank[a]; b = rank[b];
if(a > b){
int t = a;
a = b;
b = t;
}
a ++;
int t = Log[b - a + ];
return (best[t][a] > best[t][b - (<<t) + ])? best[t][b - (<<t) + ] : best[t][a];
}
}SA1,SA2;
char s[N];
int len,a[N],b[N],i,j,k,L,R,da,db;
int prime[N],mu[N],cnt,vis[N];
long long ans;
int main()
{
mu[]=;
for (i=;i<=;i++)
{
if (!vis[i])
{
prime[cnt++]=i;
mu[i]=-;
}
for (j=;j<cnt&&i*prime[j]<;j++)
{
vis[i*prime[j]]=;
if (i%prime[j]) mu[i*prime[j]]=-mu[i];
else {mu[i*prime[j]]=;break;}
}
}
int test;
scanf("%d",&test);
while (test--)
{
scanf("%s",&s);
len=strlen(s);
ans=;
for (i=;i<=len;i++) ans+=i;
for (i=;i<len;i++)
{
a[i]=s[i];
b[len-i-]=s[i];
}
SA1.input(a,len,);SA1.initRMQ();
SA2.input(b,len,);SA2.initRMQ();
for (i=;i<=len;i++)
{
j=;
while (j*i<len)
{
for (k=j+;k*i<len;k++)
if (SA1.lcp(j*i,k*i)<i) break;
L=j*i;R=min(len-,k*i-);
if (R+!=len) da=min(i,SA1.lcp(L,R+));else da=;
if (L!=) db=min(i,SA2.lcp(len--R,len-L));else db=;
L=L-db;R=R+da;
j=k;
for (k=;k*i<=R-L+;k++)
ans+=mu[k]*(R-L+-k*i+);
if (R==len-) break;
}
//printf("\n");
}
printf("%I64d\n",ans);
}
}
hdu5219 Repeating的更多相关文章
-
LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
-
[LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
-
[LeetCode] Longest Repeating Character Replacement 最长重复字符置换
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
-
[LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
-
Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
-
3. Longest Substring Without Repeating Characters(c++) 15ms
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
-
【leetcode】Longest Substring Without Repeating Characters
题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
-
Longest Substring Without Repeating Characters(C语言实现)
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
-
leetcode: longest substring without repeating characters
July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...
随机推荐
-
cookie的三种操作方法
1,jquery.cookie.js 这一篇文章已经写的很详细了: http://www.cnblogs.com/afuge/archive/2013/07/03/3169048.html 2,原生j ...
-
C# - JSON操作
Newtonsoft.dll插件 http://download.csdn.net/detail/xinping_168/4710720 洪大师二次封装: using System; using Sy ...
-
将Sublime Text3添加到右键菜单中
卸载了Sublime Text2,安装了最新版本的Sublime Text3,不过一直不在右键菜单中,每次使用都需要从打开方式中选,所以决定添加,有如下2种方法. 添加到右键菜单 方法一(推荐) 在S ...
-
vmware设置centos虚拟机nat联网(转)
今天在vmware虚拟主机中安装hearbeat,为了使用最新的版本,选用编译安装了.在编译过程中,需要连接被墙的网站下载文件,那只能用vpn,但我使用的是桥接方式联网,使用不了真实主机的vpn,于是 ...
-
source insight 中文注释为乱码解决
1. source insight 中文注释为乱码解决 http://blog.csdn.net/bingfeng1210/article/details/7527059 2. Source Insi ...
-
chap3 数组 #C
4.1 数组的基本概念 4.1.1 要点归纳 一维数组 定义: int a[10]; 数组名是一个地址常量,不允许修改. 引用: 初始化: 静态数组 static int a[10];的初值? 全部赋 ...
-
LeetCode 650 - 2 Keys Keyboard
LeetCode 第650题 Initially on a notepad only one character 'A' is present. You can perform two operati ...
-
借助Chrome和插件爬取数据
工具 Chrome浏览器 TamperMonkey ReRes Chrome浏览器 chrome浏览器是目前最受欢迎的浏览器,没有之一,它兼容大部分的w3c标准和ecma标准,对于前端工程师在开发过程 ...
-
CSS样式内容
CSS代码规范:尽量不要在标签内使用样式代码. .css文档内部声明不换行可以节省内存. 谨记: 常犯的错误是.html文档和.css样式表都写好了,但总会忘记插入样式表. 1.字体的样式 2. ...
-
kbmmw 做REST 服务签名认证的一种方式
一般对外提供提供REST 服务,由于信息安全的问题, 都要采用签名认证,今天简单说一下在KBMMW 中如何 实现简单的签名服务? 整个签名服务,模仿阿里大鱼的认证方式,大家可以根据实际情况自己修改. ...