SPOJ NSUBSTR
You are given a string S which consists of 250000 lowercase latin letters at most. We define F(x) as the maximal number of times that some string with length x appears in S. For example for string 'ababa' F(3) will be 2 because there is a string 'aba' that occurs twice. Your task is to output F(i) for every i so that 1<=i<=|S|.
Input
String S consists of at most 250000 lowercase latin letters.
Output
Output |S| lines. On the i-th line output F(i).
Example
- Input:
ababa- Output:
3
2
2
1
1- 题解:
这..比上题还简单啊,根据parent树上父节点为子节点的最大子集,直接统计size即可,
英语不好看成了求和........3WA
- #include <algorithm>
- #include <iostream>
- #include <cstdlib>
- #include <cstring>
- #include <cstdio>
- #include <cmath>
- using namespace std;
- const int N=,M=;
- char s[N];int n,last=,cnt=,cur=,dis[M],ch[M][],fa[M],size[M];
- void build(int j){
- int c=s[j]-'a';
- last=cur;cur=++cnt;
- int p=last;dis[cur]=j;
- for(;p && !ch[p][c];p=fa[p])ch[p][c]=cur;
- if(!p)fa[cur]=;
- else{
- int q=ch[p][c];
- if(dis[q]==dis[p]+)fa[cur]=q;
- else{
- int nt=++cnt;
- dis[nt]=dis[p]+;
- memcpy(ch[nt],ch[q],sizeof(ch[q]));
- fa[nt]=fa[q];fa[q]=fa[cur]=nt;
- for(;ch[p][c]==q;p=fa[p])ch[p][c]=nt;
- }
- }
- size[cur]=;
- }
- int sa[M];long long ans[N],c[N];
- void Flr(){
- int p;
- for(int i=;i<=cnt;i++)c[dis[i]]++;
- for(int i=;i<=n;i++)c[i]+=c[i-];
- for(int i=cnt;i>=;i--)sa[c[dis[i]]--]=i;
- for(int i=cnt;i>=;i--){
- p=sa[i];
- if(size[p]>ans[dis[p]])ans[dis[p]]=size[p];
- size[fa[p]]+=size[p];
- }
- }
- void work(){
- scanf("%s",s+);
- n=strlen(s+);
- for(int i=;i<=n;i++)build(i);
- Flr();
- for(int i=;i<=n;i++)printf("%lld\n",ans[i]);
- }
- int main()
- {
- //freopen("pp.in","r",stdin);
- work();
- return ;
- }
SPOJ NSUBSTR的更多相关文章
- SPOJ - NSUBSTR 后缀自动机板子
SPOJ - NSUBSTR #include<bits/stdc++.h> #define LL long long #define fi first #define se second ...
- SPOJ NSUBSTR (后缀自动机)
SPOJ NSUBSTR Problem : 给一个长度为n的字符串,要求分别输出长度为1~n的子串的最多出现次数. Solution :首先对字符串建立后缀自动机,在根据fail指针建立出后缀树,对 ...
- 【spoj NSUBSTR】 Substrings
http://www.spoj.com/problems/NSUBSTR/ (题目链接) 题意 给出一个字符串S,令${F(x)}$表示S的所有长度为x的子串出现次数的最大值.求${F(1)..... ...
- SPOJ NSUBSTR Substrings 后缀自动机
人生第一道后缀自动机,总是值得纪念的嘛.. 后缀自动机学了很久很久,先是看CJL的论文,看懂了很多概念,关于right集,关于pre,关于自动机的术语,关于为什么它是线性的结点,线性的连边.许多铺垫的 ...
- SPOJ NSUBSTR Substrings
题意 dt { font-weight: bold; margin-top: 20px; padding-left: 35px; } dd { box-shadow: 3px 3px 6px #888 ...
- SPOJ - NSUBSTR(长度为1-len的字串出现的最大次数
题意:给你一个字符串,要你输出1-len的字串出现的最大次数. /** @xigua */ #include <stdio.h> #include <cmath> #inclu ...
- 【SPOJ -NSUBSTR】Substrings 【后缀自动机+dp】
题意 给出一个字符串,要你找出所有长度的子串分别的最多出现次数. 分析 我们建出后缀自动机,然后预处理出每个状态的cnt,cnt[u]指的是u这个状态的right集合大小.我们设f[len]为长度为l ...
- SPOJ NSUBSTR Substrings ——后缀自动机
建后缀自动机 然后统计次数,只需要算出right集合的大小即可, 然后更新f[l[i]]和rit[i]取个max 然后根据rit集合短的一定包含长的的性质,从后往前更新一遍即可 #include &l ...
- spoj NSUBSTR - Substrings【SAM】
先求个SAM,然后再每个后缀的对应点上标记si[nw]=1,造好SAM之后用吧parent树建出来把si传上去,然后用si[u]更新f[max(u)],最后用j>i的[j]更新f[i] 因为每个 ...
随机推荐
- Python 线程复习
修改全局变量,设立flag来避免线程间数据冲突,低效率版 from threading import Thread import time g_num=0 g_flag = 1 def test1() ...
- iOS开发-OC中TabView的编辑
UITableView编辑 1> UITableView 编辑流程 2> UITableView 编辑步骤(四步) ① 第一步 : 让 TableView 处于编辑状态(在按钮点击事件方法 ...
- 使用SecureCRTP 连接生产环境的web服务器和数据库服务器
一.使用SecureCRTP 连接生产环境的web服务器 首先,需要知道以下参数信息: 1.web服务器的ip地址 2.服务器的端口号 3.会话连接的用户名和密码 4.服务器的用户名 ...
- (原创)不带模板的OLE输出EXCEL
目前我已知的EXCEL输出方式有3种: 1.GUI_DOWNLOAD函数输出(适用于简单无格式要求的输出). 2.OLE输出(适用于对EXCEL格式输出有特殊要求的,但是因其填充数据和设置格式是基于一 ...
- node框架express
见识到原生nodeJs服务器的恶心后,我们来用下简单好用的框架吧~ 服务器无非主要提供接口和静态文件读取,直接上代码: const express = require('express'); cons ...
- LeetCode & Q189-Rotate Array-Easy
Array Description: Rotate an array of n elements to the right by k steps. For example, with n = 7 an ...
- Python扩展模块——调用WindowsAPI(pywin32的简单使用)
这块使用的比较少,只用到了模拟键盘按键, 调用鼠标比较费事,是通过像素坐标实现的,如果没有特殊需求或万不得已不建议使用 import win32con import win32api win32api ...
- 解决SoapFault (looks like we got no XML document)问题
今天在调试项目的时候出现下面的错误信息: SoapFault looks like we got no XML document (D:\phpStudy\WWW\self.shop.xunmall. ...
- 三十天学不会TCP,UDP/IP网络编程 -- RTT的计算
欢迎去gitbook(https://www.gitbook.com/@rogerzhu/)看到完整版. 如果对和程序员有关的计算机网络知识,和对计算机网络方面的编程有兴趣,虽然说现在这种“看不见”的 ...
- shell:正则表达式和文本处理器
1.什么是正则 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则. 生活中处处都是正则: 比如我们描述:4条腿 你可能会想 ...