洛谷 P3435 [POI2006]OKR-Periods of Words(kmp)

P3435 [POI2006]OKR-Periods of Words

题目描述
A string is a finite sequence of lower-case (non-capital) letters of the English alphabet. Particularly, it may be an empty sequence, i.e. a sequence of 0 letters. By A=BC we denotes that A is a string obtained by concatenation (joining by writing one immediately after another, i.e. without any space, etc.) of the strings B and C (in this order). A string P is a prefix of the string !, if there is a string B, that A=PB. In other words, prefixes of A are the initial fragments of A. In addition, if P!=A and P is not an empty string, we say, that P is a proper prefix of A.

A string Q is a period of Q, if Q is a proper prefix of A and A is a prefix (not necessarily a proper one) of the string QQ. For example, the strings abab and ababab are both periods of the string abababa. The maximum period of a string A is the longest of its periods or the empty string, if A doesn’t have any period. For example, the maximum period of ababab is abab. The maximum period of abc is the empty string.

Task Write a programme that:

reads from the standard input the string’s length and the string itself,calculates the sum of lengths of maximum periods of all its prefixes,writes the result to the standard output.

一个串是有限个小写字符的序列,特别的,一个空序列也可以是一个串. 一个串P是串A的前缀, 当且仅当存在串B, 使得 A = PB. 如果 P A 并且 P 不是一个空串,那么我们说 P 是A的一个proper前缀. 定义Q 是A的周期, 当且仅当Q是A的一个proper 前缀并且A是QQ的前缀(不一定要是proper前缀). 比如串 abab 和 ababab 都是串abababa的周期. 串A的最大周期就是它最长的一个周期或者是一个空串(当A没有周期的时候), 比如说, ababab的最大周期是abab. 串abc的最大周期是空串. 给出一个串,求出它所有前缀的最大周期长度之和.。

输入输出格式
输入格式:
In the first line of the standard input there is one integer kk ( 1\le k\le 1\ 000\ 0001≤k≤1 000 000 ) - the length of the string. In the following line a sequence of exactly kk lower-case letters of the English alphabet is written - the string.

输出格式:
In the first and only line of the standard output your programme should write an integer - the sum of lengths of maximum periods of all prefixes of the string given in the input.

输入输出样例
输入样例#1:
8
babababa
输出样例#1:
24

分析:看了一下发现就是求每个前缀最小的公共前后缀长度x,i-x就是最大周期。

代码

#include <cstdio>
#include <string>
#include <cstring>
#define N 1000005
using namespace std;

char s[N];
long long next[N],n,ans;

int main()
{
    scanf("%d" ,&n);
    scanf("%s", s + 1);
    int j = 0;
    for (int i = 2; i <= n; i++)
    {
        while (j && s[j + 1] != s[i]) j = next[j];
        if (s[j + 1] == s[i]) j++;
        next[i] = j;
    }
    for (int i = 1; i <= n; i++)
    {
        int p = i;
        while (next[p]) p = next[p];
        if (next[i]) next[i] = p;
        ans+=i - p;
    }
    printf("%lld",ans);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值