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)
上一篇 2022年1月14日 下午2:00
下一篇 2022年1月14日 下午2:00


相关推荐

  • Openclaw 小龙虾电脑配置要求

    Openclaw 小龙虾电脑配置要求

    2026年3月13日
    3
  • n8n打造属于你的开源自动化工作流平台(完整指南)

    n8n打造属于你的开源自动化工作流平台(完整指南)

    2026年3月15日
    2
  • Spring Boot缓存注解@Cacheable、@CacheEvict、@CachePut使用

    Spring Boot缓存注解@Cacheable、@CacheEvict、@CachePut使用从 3 1 开始 Spring 引入了对 Cache 的支持 其使用方法和原理都类似于 Spring 对事务管理的支持 SpringCache 是作用在方法上的 其核心思想是这样的 当我们在调用一个缓存方法时会把该方法参数和返回结果作为一个键值对存放在缓存中 等到下次利用同样的参数来调用该方法时将不再执行该方法 而是直接从缓存中获取结果进行返回 所以在使用 SpringCache 的时候我们要保证我们缓存的方法对

    2026年3月19日
    2
  • 教你写Makefile(很全,含有工作经验的)

    教你写Makefile(很全,含有工作经验的)原文转载文Makefile值得一提的是,在Makefile中的命令,必须要以[Tab]键开始。    什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和professional的程序员,makefile还是要懂。这就好像现在有这么多的HTML的编辑器,但如果你想成为一个专业人士,你还是…

    2022年5月8日
    33
  • eNSP常用命令 华为模拟器eNSP常用命令

    eNSP常用命令 华为模拟器eNSP常用命令路由器常用命令 进入任务视图给路由器取名 进入指定接口 给当前路由器接口配置 IP 地址和子网掩码 退出接口或系统视图 启用 DHCP 指定该接口拥有 DHCP 功能 指定 DNS 服务器的 IP 地址 显示全部 ip 的路由表 显示指定 ip 路由表 添加静态路由 交换机常用命令 交换机改变语言模式 创建 vlan 查看所有 vlan 将接口拆分为多个子接口 指定接口与哪个 vlan 关联 启用 arp 广播 将接口修改为 access 接口 将接口修改为 trunk 接口 将接口划分到指定 vlan 里 查看开启 stp 后的交换机接口的接口情况 查看交换

    2026年3月16日
    1
  • HttpClient详细使用示例「建议收藏」

    HttpClient详细使用示例「建议收藏」HTTP协议可能是现在Internet上使用得最多、最重要的协议了,越来越多的Java应用程序需要直接通过HTTP协议来访问网络资源。虽然在JDK的javanet包中已经提供了访问HTTP协议的基本功能,但是对于大部分应用程序来说,JDK库本身提供的功能还不够丰富和灵活。HttpClient是ApacheJakartaCommon下的子项目,用…

    2022年7月19日
    20

发表回复

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

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