zoukankan      html  css  js  c++  java
  • [POI2006]OKR-Periods of Words(KMP)

    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 (1le kle 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.

    样例输入:

    8

    babababa

    样例输出:

    24

    题目大意:

    一个串是有限个小写字符的序列,特别的,一个空序列也可以是一个串. 一个串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的最大周期是空串. 给出一个串,求出它所有前缀的最大周期长度之和.。

    这个题并非朴素求next数组,一开始读题没读懂题意,最后will佬说这种题是一眼题..

    举个例子:aba的proper period是ab,因为ab是aba的前缀且aba是abab的周期。

    交叉重叠部分就是题目要求的周期

    朴素的next数组为:0 0 1 2 3 4 5 6

    正确的next数组应该是用长度减掉两段交叉的部分(两段交叉的部分是最小周期,所以剪掉后就是最大的题目要求的周期)

    即对next求出后再进行一次处理,将后面的指向前面的找到交叉部分然后用长度减掉。

    b:0
    ba:0
    bab:1
    baba:2
    babab:3
    bababa:4
    bababab:5
    babababa:6

    比如bababa这个前缀吧,它最小的proper是baba,next=2,也就是指向ba,所以baba的最大周期是6-2=4,意思是从ba开始跳要跳4才能到bababa,最大周期是4,。

    哈?最小周期?最小周期就是6-4=2。

     1 #include<cstdio>
     2 #include<cstring>
     3 using namespace std;
     4 long long ans;
     5 char s[1000005];
     6 int next[1000005];
     7 int main(){
     8     int n;
     9     scanf("%d",&n);
    10     scanf("%s",s+1);
    11     int j=0;
    12     for(int i=2;i<=n;i++){
    13         while(j!=0&&s[i]!=s[j+1]){
    14             j=next[j];    
    15         }
    16         if(s[i]==s[j+1]){
    17             j++; 
    18             next[i]=j;    
    19         } 
    20     }
    21     for(int i=1;i<=n;i++){
    22         if(next[next[i]]!=0){
    23             next[i]=next[next[i]];
    24         }
    25     }
    26     for(int i=1;i<=n;i++){
    27         if(next[i]!=0){
    28             ans+=i-next[i];
    29         }
    30     }
    31     printf("%lld",ans);
    32     return 0;
    33     
    34 }
  • 相关阅读:
    手动安装ceph集群
    openstack端口禁用安全规则
    debian10桌面美化
    debian10 制作动态扩容根分区镜像
    CentOS7制作动态扩容根分区镜像
    EFKLK日志收集工具栈
    ceph rbd(csi) 对接 kubernetes sc
    secureCRT 814配色方案
    python function
    Linux操作篇之LNMP(一)
  • 原文地址:https://www.cnblogs.com/Fylsea/p/7805507.html
Copyright © 2011-2022 走看看