• BZOJ-1511: [POI2006]OKR-Periods of Words (傻逼KMP)


    1511: [POI2006]OKR-Periods of Words

    Time Limit: 5 Sec  Memory Limit: 64 MB
    Submit: 323  Solved: 194
    [Submit][Status][Discuss]

    Description

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

    Input

    第一行一个整数 k ( 1 k 1 000 000) 表示串的长度. 接下来一行表示给出的串.

    Output

    输出一个整数表示它所有前缀的最大周期长度之和.

    Sample Input

    8
    babababa

    Sample Output

    24

    HINT

     

    Source

    mdzz竟然是权限题……fuck!

     1 #include "bits/stdc++.h"
     2 using namespace std;
     3 typedef long long LL;
     4 const int MAX=1e6+5;
     5 int len,next[MAX];
     6 char s[MAX];
     7 int main(){
     8     freopen ("word.in","r",stdin);freopen ("word.out","w",stdout);
     9     int i,j;
    10     scanf("%d
    %s",&len,s);
    11     j=next[0]=-1;i=0;
    12     while (i<=len){
    13         if (j==-1 || s[i]==s[j]) next[++i]=++j;
    14         else j=next[j];
    15     }
    16     for (i=1;i<=len;i++)
    17         if (next[i])
    18             while (next[next[i]]) next[i]=next[next[i]];
    19     LL ans=0;
    20     for (i=1;i<=len;i++)
    21         if (next[i])
    22             ans+=(LL)(i-next[i]);
    23     printf("%lld",ans);
    24     return 0;
    25 }
  • 相关阅读:
    Linux 7 web服务基础知识
    Linux 6 Nginx
    Linux 5 MySQL、redis相关
    Linux 4 安装相关程序
    phpcms 路由配置
    ecmall 入口文件解析 引入了什么
    php 调用天气接口
    phpcms 加载微信类库,生成签名
    ecmall 学习记录2
    Jquery 遍历
  • 原文地址:https://www.cnblogs.com/keximeiruguo/p/7806400.html
Copyright © 2020-2023  润新知