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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 配置静态路由,动态路由,默认路由模式_默认路由为网络和掩码

    配置静态路由,动态路由,默认路由模式_默认路由为网络和掩码一、什么是路由路由(routing)是指分组从源到目的地时,决定端到端路径的网络范围的进程[1]。路由工作在OSI参考模型第三层——网络层的数据包转发设备。路由器通过转发数据包来实现网络互连。虽然路由器可以支持多种协议(如TCP/IP、IPX/SPX、AppleTalk等协议),但是在我国绝大多数路由器运行TCP/IP协议。路由器通常连接两个或多个由IP子网或点到点协议标识的…

    2022年9月16日
    0
  • 网站响应时间过长的原因及解决方法

    网站响应时间过长的原因及解决方法

    2021年10月14日
    180
  • 排序算法最坏情况下的比较次数怎么算_选择排序最坏情况

    排序算法最坏情况下的比较次数怎么算_选择排序最坏情况在最坏情况下,冒泡排序、直接插入排序与简单选择排序法均需要比较n(n-1)/2次。希尔排序需要比较n^1.5次,堆排序需要比较的次数最少,为nlog2n。

    2022年8月23日
    3
  • Linux下c语言多线程编程

    Linux下c语言多线程编程创建线程函数pthread_create()和等待线程函数pthread_join()的用法。注意:在创建线程pthread_create()之前,要先定义线程标识符:pthread_t自定义线程名;例子1:创建线程以及等待线程执行完毕。#include<stdio.h>#include<stdlib.h>#include<pthread.h>//线程要运行的函数,除了函数名myfunc,其他全都是固定的。void*myfunc(){ p

    2022年10月21日
    0
  • FileSystemWatcher 监控文件变化

    FileSystemWatcher 监控文件变化本文测试了FileSystemWatcher类监控文件变化。usingSystem;usingSystem.Security.Permissions;usingSystem.IO;namespaceConsoleApp1{publicclassFileStateWatcher{[PermissionSet(Secu…

    2022年6月16日
    56
  • luke的使用[通俗易懂]

    luke的使用[通俗易懂]zz网络首次用Luke打开索引文件。Overview里面显示了索引具有的Field数目(以最大的Field为准吧?),还有就是文档的总数和Term即索引词的总数?有时一个关键词,对应于多个文档,所

    2022年8月6日
    3

发表回复

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

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