【POJ11855】 Buzzwords (后缀数组)
Description
The word “the” is the most common
three-letter word. It even
shows up inside other words, such
as “other” and “mathematics”.
Sometimes it hides, split between
two words, such as “not here”.
Have you ever wondered what the
most common words of lengths
other than three are?
Your task is the following. You
will be given a text. In this text,
find the most common word of
length one. If there are multiple
such words, any one will do. Then
count how many times this most
common word appears in the text. If it appears more than once, output how many times it appears.
Then repeat the process with words of length 2, 3, and so on, until you reach such a length that
there is no longer any repeated word of that length in the text.Input
The input consists of a sequence of lines. The last line of input is empty and should not be processed.
Each line of input other than the last contains at least one but no more than one thousand uppercase
letters and spaces. The spaces are irrelevant and should be ignored.Output
For each line of input, output a sequence of lines, giving the number of repetitions of words of length
1, 2, 3, and so on. When you reach a length such that there are no repeated words of that length,
output one blank line, do not output anything further for that input line, and move on to the next line
of input.
Note: Remember that the last line of the sample input and of the sample output must be blank.Sample Input
THE OTHER MATHEMATICS NOT HERE
AASample Output
5
4
4
2
2
2
【题意】
给定一个文本,求出长度为1, 2, 3, 4, 5....的字符串最大出现次数,一直找到出现次数不大于1为止。
【分析】
直接for两遍。按枚举的长度分组,求出小组成员个数的max即可。
代码如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxl 100010
#define INF 0xfffffff int l,len;
char s[Maxl];
int c[Maxl],cl; int mymin(int x,int y) {return x<y?x:y;}
int mymax(int x,int y) {return x>y?x:y;} void init()
{
len=strlen(s);
cl=;
for(int i=;i<len;i++) if(s[i]!=' ')
c[++cl]=s[i]-'A'+;
} int sa[Maxl],rk[Maxl],y[Maxl],wr[Maxl],Rs[Maxl];
void get_sa(int m)
{
memcpy(rk,c,sizeof(rk));
for(int i=;i<=m;i++) Rs[i]=;
for(int i=;i<=cl;i++) Rs[rk[i]]++;
for(int i=;i<=m;i++) Rs[i]+=Rs[i-];
for(int i=cl;i>=;i--) sa[Rs[rk[i]]--]=i; int ln=,p=;
while(p<cl)
{
int k=;
for(int i=cl-ln+;i<=cl;i++) y[++k]=i;
for(int i=;i<=cl;i++) if(sa[i]>ln) y[++k]=sa[i]-ln;
for(int i=;i<=cl;i++) wr[i]=rk[y[i]]; for(int i=;i<=m;i++) Rs[i]=;
for(int i=;i<=cl;i++) Rs[wr[i]]++;
for(int i=;i<=m;i++) Rs[i]+=Rs[i-];
for(int i=cl;i>=;i--) sa[Rs[wr[i]]--]=y[i]; for(int i=;i<=cl;i++) wr[i]=rk[i];
for(int i=cl+;i<=cl+ln;i++) wr[i]=;
p=,rk[sa[]]=;
for(int i=;i<=cl;i++)
{
if(wr[sa[i]]!=wr[sa[i-]]||wr[sa[i]+ln]!=wr[sa[i-]+ln]) p++;
rk[sa[i]]=p;
}
ln*=,m=p;
}
sa[]=rk[]=;
} int height[Maxl];
void get_he()
{
int k=;
for(int i=;i<=cl;i++) if(rk[i]!=)
{
int j=sa[rk[i]-];
if(k) k--;
while(c[i+k]==c[j+k]&&i+k<=cl&&j+k<=cl) k++;
height[rk[i]]=k;
}
} void ffind()
{
for(int i=;i<=cl;i++)//枚举长度i
{
int cnt=,ans=;
for(int j=;j<=cl;j++)
{
cnt++;
if(height[j+]<i||j==cl)//是一组的结束
{
if(cnt!=) ans=mymax(ans,cnt);
cnt=;
}
}
if(ans<=) break;
printf("%d\n",ans);
}
} int main()
{
bool ok=;
while(gets(s))
{
if(ok) printf("\n");
ok=;
init();
get_sa();
get_he();
ffind();
}
return ;
}
[UVA11855]
2016-07-19 16:31:07
【POJ11855】 Buzzwords (后缀数组)的更多相关文章
- 后缀数组的倍增算法(Prefix Doubling)
后缀数组的倍增算法(Prefix Doubling) 文本内容除特殊注明外,均在知识共享署名-非商业性使用-相同方式共享 3.0协议下提供,附加条款亦可能应用. 最近在自学习BWT算法(Burrows ...
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
- BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]
1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1383 Solved: 582[Submit][St ...
- POJ3693 Maximum repetition substring [后缀数组 ST表]
Maximum repetition substring Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9458 Acc ...
- POJ1743 Musical Theme [后缀数组]
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 27539 Accepted: 9290 De ...
- 后缀数组(suffix array)详解
写在前面 在字符串处理当中,后缀树和后缀数组都是非常有力的工具. 其中后缀树大家了解得比较多,关于后缀数组则很少见于国内的资料. 其实后缀数组是后缀树的一个非常精巧的替代品,它比后缀树容易编程实现, ...
- 【UOJ #35】后缀排序 后缀数组模板
http://uoj.ac/problem/35 以前做后缀数组的题直接粘模板...现在重新写一下模板 注意用来基数排序的数组一定要开到N. #include<cstdio> #inclu ...
- 【BZOJ-2119】股市的预测 后缀数组
2119: 股市的预测 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 334 Solved: 154[Submit][Status][Discuss ...
- 【BZOJ-4698】Sandy的卡片 后缀数组
4698: Sdoi2008 Sandy的卡片 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 140 Solved: 55[Submit][Stat ...
- POJ 1743 Musical Theme ——后缀数组
[题目分析] 其实找最长的不重叠字串是很容易的,后缀数组+二分可以在nlogn的时间内解决. 但是转调是个棘手的事情. 其实只需要o(* ̄▽ ̄*)ブ差分就可以了. 背板题. [代码] #include ...
随机推荐
- Java基础知识强化之集合框架笔记54:Map集合之HashMap集合(HashMap<String,String>)的案例
1. HashMap集合 HashMap集合(HashMap<String,String>)的案例 2. 代码示例: package cn.itcast_02; import java.u ...
- CentOS 6.7配置Nginx 1.8负载均衡
本教程使用Vultr的VPS搭建,准备三台VPS,一主两从 master - 45.32.90.100 slave1 - 45.32.92.47 slave2 - 45.32.89.205 1.编译安 ...
- java Date日期去掉时分秒
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");String s = sdf.format(new Date() ...
- 利用jQuery npoi插件 asxh一般处理文件实现excel的下载
最近开发的过程中遇到这么一个问题,利用ajax和ashx文件实现下载功能.发现代码调试走完之后并没有弹出下载框. 研究了一段时间之后发现解决这种问题有两种方法,1.ajax获取数据集在前台做处理实现导 ...
- 反射+javacsv+scv文件构建资源获取
1.反射之前已经写过,大家可以在http://www.cnblogs.com/myadmin/p/5282628.html中大概了解下.同时请大家指正. javacsv.jar:java中用来处理cs ...
- python基础知识五
数据结构基本上就是---它们可以处理一些数据的结构.或者说,它们是用来存储一组相关数据的. python中有三种内建的数据结构---列表.元祖和字典. 我们将会学习如何使用它们,以及它们如何使编程变得 ...
- javascript类继承系列一
js中没有提供类(class,抽象类,接口等高级的抽象),可以用new,但new的function的对象,构造器 但在js中可以通过function来模拟类的一些特性function fun_name ...
- ROW_NUMBER () 与 PARTITION组合拳
--在一个Book表里面里有字段AuthorID与Author表关联,现在要求按PublishDate字段倒序排列,列出每个作者的前五本书.要求有没有一条语句搞定的--可用游标或者临时表--最好解决方 ...
- CSS jQuery HTML5 CSS3
jquery css3图片文字介绍鼠标滚动页面动画单页 http://www.17sucai.com/preview/1/2013-12-30/%E5%8A%A8%E7%94%BB%E5%8D%95% ...
- .NET 设计模式之简单工厂模式(二)
1:建立接口 namespace Factory { public interface IPerson { } } 2:建立Worker.Student来继承IPerson接口 namespace F ...