Codeforces 432 D. Prefixes and Suffixes

Codeforces 432 D. Prefixes and Suffixes

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

随着扩展KMP做一个简单的努力…..

D. Prefixes and Suffixes
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You have a string s = s1s2s|s|, where |s| is the length of string s, and si its i-th character.

Let’s introduce several definitions:

  • A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string sisi + 1sj.
  • The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l].
  • The suffix of string s of length l (1 ≤ l ≤ |s|) is string s[|s| - l + 1..|s|].

Your task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring.

Input

The single line contains a sequence of characters s1s2s|s| (1 ≤ |s| ≤ 105) — string s. The string only consists of uppercase English letters.

Output

In the first line, print integer k (0 ≤ k ≤ |s|) — the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers li ci. Numbers li ci mean that the prefix of the length li matches the suffix of length li and occurs in string s as a substringci times. Print pairs li ci in the order of increasing li.

Sample test(s)
input
ABACABA

output
3
1 4
3 2
7 1

input
AAA

output
3
1 3
2 2
3 1

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn=100100;

char T[maxn],P[maxn];
int next[maxn],ex[maxn];

void pre_exkmp(char P[])
{
    int m=strlen(P);
    next[0]=m;
    int j=0,k=1;
    while(j+1<m&&P[j]==P[j+1]) j++;
    next[1]=j;
    for(int i=2;i<m;i++)
    {
        int p=next[k]+k-1;
        int L=next[i-k];
        if(i+L<p+1) next[i]=L;
        else
        {
            j=max(0,p-i+1);
            while(i+j<m&&P[i+j]==P[j]) j++;
            next[i]=j; k=i;
        }
    }
}

void exkmp(char P[],char T[])
{
    int m=strlen(P),n=strlen(T);
    pre_exkmp(P);
    int j=0,k=0;
    while(j<n&&j<m&&P[j]==T[j]) j++;
    ex[0]=j;
    for(int i=1;i<n;i++)
    {
        int p=ex[k]+k-1;
        int L=next[i-k];
        if(i+L<p+1) ex[i]=L;
        else
        {
            j=max(0,p-i+1);
            while(i+j<n&&j<m&&T[i+j]==P[j]) j++;
            ex[i]=j; k=i;
        }
    }
}

int pos[maxn],sum[maxn],mx=-1;

struct ANS
{
    int a,b;
}ans[maxn];
int na=0;

bool cmp(ANS x,ANS y)
{
    if(x.a!=y.a)return x.a<y.a;
    return x.b<y.b;
}

int lisan[maxn],nl;

int main()
{
    cin>>P;
    pre_exkmp(P);
    int n=strlen(P);
    for(int i=0;i<n;i++)
    {
        pos[next[i]]++;
        lisan[nl++]=next[i];
        mx=max(mx,next[i]);
    }
    sort(lisan,lisan+nl);
    int t=unique(lisan,lisan+nl)-lisan;
    for(int i=t-1;i>=0;i--)
    {
        sum[lisan[i]]=sum[lisan[i+1]]+pos[lisan[i]];
    }
    for(int i=0;i<n;i++)
    {
        if(next[i]==n-i)
        {
            ans[na++]=(ANS){next[i],sum[next[i]]};
        }
    }
    sort(ans,ans+na,cmp);
    printf("%d\n",na);
    for(int i=0;i<na;i++)
    {
        printf("%d %d\n",ans[i].a,ans[i].b);
    }
    return 0;
}

版权声明:来自: 代码代码猿猿AC路 http://blog.csdn.net/ck_boss

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116863.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Tomcat日志乱码问题解决方法

    Tomcat日志乱码问题解决方法问题描述:启动tomcat之后,控制台打印的日志中出现了中文乱码的情况:解决方法1.找到tomcat下的conf目录下的logging.properties文件。2.将logging.properties用记事本打开,然后将java.util.logging.ConsoleHandler.encoding等号后的UTF-8改为GBK。…

    2022年9月26日
    1
  • 3D视频编码(3d打印技术介绍)

    3D-HEVC编码框架3D-HEVC编码结构是对HEVC的扩展,每个视点纹理及深度图编码主要采用HEVC编码框架,但在其基础上增加了一些新的编码技术,使其更有利于深度图和多视点的编码。图13D-HEVC编码结构如上图所示,3D-HEVC编解码结构和MVC类似。图中所有输入的视频图像和深度图像是同一时刻,不同拍摄位置的场景,这些图像组成一个存取层。在同一个存取层中,首先对独立视点(基准视点…

    2022年4月13日
    50
  • 网络安全与渗透测试工具导航下载_渗透师导航

    网络安全与渗透测试工具导航下载_渗透师导航一些网络安全与渗透测试工具导航,值得收藏,看看有没有你熟悉的,也许有一天你会用得到! 入门指南 在线靶场 文件上传漏洞靶场 导航 payload 子域名枚举 自动爬虫实现的子域名收集工具 waf开源及规则 web应用扫描工具 webshell检测以及病毒分析 DDos防护 Android系列工具 XSS扫描 代码审计 端口扫描、指

    2022年8月12日
    5
  • linux复制/剪切文件到另一个文件夹「建议收藏」

    linux复制/剪切文件到另一个文件夹「建议收藏」复制/拷贝:cp文件名路径cphello.csv./python/ml:把当前目录的hello.csv拷贝到当前目的python文件夹里的ml文件夹里cp源文件名新文件名cphello.txtworld.txt:复制并改名,并存放在当前目录下cpfile1file2复制一个文件cpdir/*.复制一个目录下的所有文件…

    2022年8月23日
    19
  • MATLAB优化函数fmincon解析[通俗易懂]

    MATLAB优化函数fmincon解析[通俗易懂]MATLAB,优化函数fmincon解析[x,fval,exitflag,output,lambda,grad,hessian]=fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);…

    2022年6月14日
    237
  • 循环单链表解决约瑟夫环_用链表解决约瑟夫环问题

    循环单链表解决约瑟夫环_用链表解决约瑟夫环问题已知有5个人围坐在一张圆桌的周围,从编号为3的人开始顺时针数数,数到2的那个人出列淘汰,然后从出列的下个一人继续数,依次循环,直到只剩下最后一个人。(使用循环链表实现约瑟夫环)代码如下:#include “pch.h”#include<string>#include<fstream>#include<Windows.h>#include <i…

    2022年8月18日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号