【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 ...
随机推荐
- pugixml
http://www.firedragonpzy.com.cn/index.php/archives/3227 有关cocos2d-x的xml文件读取问题
- 关于js中return false、event.preventDefault()和event.stopPropagation()
在平时项目中,如果遇到需要阻止浏览器默认行为,大家经常会用return false;和event.preventDefault()来阻止,但对它俩的区别还是有些一知半解,于是看了文档,查了些资料,在此 ...
- Eclipse3.7中搭建Android开发环境文档教程和视频教程
1.下载Eclipse3.7,登录http://www.eclipse.org/downloads/,下载Eclipse Classic 3.7: 2.安装ADT插件:下载好Eclipse后解压,运行 ...
- 批处理,修改环境变量path的方法(加环境变量)
方法一:批处理中,修改环境变量,一次性有效(也就是在当前的脚本中有效) CMD中运行 set path==%path%;d:/mypath 用 set path可以查看,当前的环境变量 方法二 :批处 ...
- homework做了些什么?
第一步:get_new_guid_uid_pairs_{$ymd} 参数是时间和100上的文件. 那么100上的文件是从哪里来的呢? 我们进入到100机器上,打开root权限下的cron,看到如下内容 ...
- php快速定位多维数组的深度
原文地址:php快速定位多维数组的深度作者:陌上花开 自定义一个函数: function array_depth($array) { $max_depth = 1; foreach ($array ...
- gitcafe 使用hexo搭建博客
--缘由:因为看大家都用github等搭建博客,作为半个程序员的自己,也按捺不住了,终于有空来尝试一把了,选择了和github 相同功能的gitcafe网站,因为在国内比较快,这是大家的看法,下面写一 ...
- php字符串函数(1)
下面去学习一下php的字符串函数,那么怎么去看手册呢,举个例子 int strcasecmp ( string $str1 , string $str2 ) 第一个int,表示此函数返回的类型是int ...
- 网站集A的子网站B上没有解决方案C发布的webpart
在A的主站点,查看解决方案C在该网站集的feature,若未打开,则打开:若已经打开,可以先关闭再打开一次. 如果问题依然存在,可以查看子网站B上的feature是否打开,若未打开,则打开:若已经打开 ...
- Redis介绍
Redis的介绍 Remote Dictionary Server(Redis)是一个基于 key-value 键值对的持久化数据库存储系统.支持多种数据结构,包括 string (字符串).list ...