前缀

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 A, 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.

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

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

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

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

输入格式
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复制

1
8babababa

输出 #1复制

24

题意:

对于给定串S的每个前缀i(记为ai)ai可以等于A
求ai的满足条件的Q
求所有的这些“前缀的前缀”Q的长度和L

Q满足:Q是ai的前缀,Q!=ai,Q+Q能够覆盖ai,且ai是Q+Q的前缀

 这里的代码是学习的,原代码地址:https://www.luogu.com.cn/blog/dedicatus545/solution-p3435

复制代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
char a[1000010];
int n,f[1000010];
int main(){
    scanf("%d",&n);//字符串长度
    scanf("%s",a); //给定的字符串
    int i,j;
    ll cnt=0;//记录最终答案
    f[0]=f[1]=0;
    j=0;
    for(i=1;i<n;i++)
    {//求解next
        while(j&&(a[i]!=a[j])) j=f[j];
        j+=(a[i]==a[j]);
        f[i+1]=j;
    }
    /*
    for(int i=0;i<n;i++)
    {
        cout<<f[i]<<" ";
    }
    cout<<endl;
    */
    for(i=1;i<=n;i++)
    {
          j=i;
          //cout<<i<<": "<<"fail["<<i<<"]="<<f[i]<<" ";
          while(f[j]) j=f[j];
          //cout<<"j="<<j<<";";
          if(f[i]!=0) f[i]=j;//记忆化
          //cout<<"fail["<<i<<"]="<<f[i]<<";";
          //cout<<i-j<<endl;

          cnt+=i-j;

    }
    //cout<<endl;
    printf("%lld",cnt);
}
复制代码

 

 

下面是分析:

例如题目给的样例:
A=babababa

(注图片里是A的所有前缀,不是所有proper前缀,这里更改一下)

 

 

 代码里next与题目之间的联系:

最长周期=字符串长度—最短前后缀长度

              =字符串长度—用next算出的前缀的前缀长度

 然后我们看一下编程

 

 

 

posted @   VonSafen  阅读(162)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示