CodeForces - 432D 后缀数组

You have a string s = s1s2…s|s|, where |s| is the length of string s, and si its i-th character.

Let’s introduce several definitions:

A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string sisi + 1…sj.
The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l].
The suffix of string s of length l (1 ≤ l ≤ |s|) is string s[|s| - l + 1..|s|].
Your task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring.

Input
The single line contains a sequence of characters s1s2…s|s| (1 ≤ |s| ≤ 105) — string s. The string only consists of uppercase English letters.

Output
In the first line, print integer k (0 ≤ k ≤ |s|) — the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers li ci. Numbers li ci mean that the prefix of the length li matches the suffix of length li and occurs in string s as a substring ci times. Print pairs li ci in the order of increasing li.

Example
Input
ABACABA
Output
3
1 4
3 2
7 1
Input
AAA
Output
3
1 3
2 2
3 1

给定一个串的定义,在原串前缀和后缀相等的才算一个串,并且找出这样的串在原串中出现的次数。

用后缀数组 找出sa[i] 为0的串所在的排名,说明是从0开始的,然后往上往下找,满足有相同的长度并且这种长度必须有其中的一个
sa[i]+height[i]〉=len 这个串成立。然后后缀数组有个特殊的性质,就是长度大的必定对长度小的有贡献,那么就是最后记录一下后缀和就好
。。手残党一开始写的时候想着各种用set map 存下来哪种串可行,但是遍历前后是有顺序的这样做不可行,所以直接数组标记长度就好,存下所有的和,
那么往前往后只要有一个在此长度可行的就好。

首先利用后缀数组处理出sa[i]代表排名第i位的后缀的起始位置
处理出rank[i]代表第i个位置起始的后缀的rank
处理出height[i]代表排名第i位的和排名i-1位的公共前缀的长度。
那么我们要找后缀和前缀相等的就是找到rank[0],然后按照排名,向前向后遍历,任意两个后缀的公共前缀就是他们[i,j]区间内所有height的最小值。只要得到的公共前缀等于后缀的长度,那么证明前缀和后缀匹配上。记录该长度的前缀能够匹配

记录某一个子串出现的次数,就是每次记录和原字符串的公共前缀的长度,然后在对应位置+1,因为比这个长度小的也会得到一个贡献,所以最后求一下后缀和即可。

include

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值