hatredvirus

导航

 

题目描述

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.

输入格式

In the first line of the standard input there is one integer kk (1\le k\le 1\ 000\ 0001k1 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.

题意翻译

对于一个仅含小写字母的字符串 a,p 为 a 的前缀且p≠a,那么我们称 p 为 a 的 proper 前缀。

规定字符串 Q(可以是空串)表示 a 的周期,当且仅当 Q 是 a 的 proper 前缀且 a 是 Q+Q 的前缀。

例如 ab 是 abab 的一个周期,因为 ab 是 abab 的 proper 前缀,且 abab 是 ab+ab 的前缀。

求给定字符串所有前缀的最大周期长度之和。

输入输出样例

输入 #1          输出#1

8                      24

babababa

 

解析:

关于next数组:对于模式串的某一位置 j,next[j] 的值是该模式串从下标 0到 j - 1的子串最大相等前缀与后缀数;即前缀j的长度为next[j]的前缀和后缀是相等的

以题中给出的案例来说,对于字符串babababa,它的前缀有:b,ba,bab,baba,babab,bababa,bababab,babababa。我们从零开始计数,接下来对其一个个分析:

0:b:无前缀,显然next[0]=0

1:ba:proper前缀为b,但不满足周期的定义(ba不是b+b=bb的前缀)故next[1]=0

2:bab:proper前缀为b,ba。对于b,同上,不满足周期;而对于ba,bab是ba+ba=baba的前缀,故ba是bab的一个周期,next[2]=2

3:baba:proper前缀为b,ba,bab。对于b和bab,不满足周期;对于ba,baba是ba+ba=baba的前缀,所以next[3]=2

4:babab:proper前缀为b,ba,bab,baba。前面的同上;对于baba,babab是baba+baba=babababa的前缀,所以next[4]=4

5:bababa:proper前缀为b,ba,bab,baba,babab。同上,对于b,bab,babab,不满足周期;对于baba,满足bababa是baba+baba=babababa的前缀,所以next[5]=4

6:bababab:proper前缀为b,ba,bab,baba,babab,bababa。同上,对于bababa,bababab是bababa+bababa=babababababa的前缀,所以next[6]=6

7:babababa:proper前缀为b,ba,bab,baba,babab,bababa,bababab.同上,对于b,bab,babab,bababab,不满足周期;对于bababa,babababa是bababa+bababa=babababababa的前缀,所以next[7]=6

综上可得,next[0]=next[1]=0;next[2]=next[3]=2;next[4]=next[5]=4;next[6]=next[7]=6

所要求的和就是上面的数字相加,即0+0+2+2+4+4+6+6=24

 

代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define ll long long

using namespace std;

char c[1000001];
int n,next11[1000001];

int main(){
    
    cin>>n>>c;int i,j;ll s=0;
    next11[0]=next11[1]=0;j=0;
    for(i=1;i<n;i++)//求解next数组的值
    {
        while(j&&(c[i]!=c[j])) j=next11[j];
        j+=(c[i]==c[j]);next11[i+1]=j;
    }
    for(i=1;i<=n;i++)
    {
        j=i;
        while(next11[j]) j=next11[j];
        if(next11[i]!=0) next11[i]=j;//记忆化
        s+=i-j;
    }
    cout<<s;
    return 0;
}

  (代码参考洛谷解析而来)

posted on 2022-04-29 20:40  HatredVirus  阅读(34)  评论(0编辑  收藏  举报