【POJ11855】 Buzzwords (后缀数组)

时间:2022-09-25 10:54:40

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
AA

Sample 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 (后缀数组)的更多相关文章

  1. 后缀数组的倍增算法(Prefix Doubling)

    后缀数组的倍增算法(Prefix Doubling) 文本内容除特殊注明外,均在知识共享署名-非商业性使用-相同方式共享 3.0协议下提供,附加条款亦可能应用. 最近在自学习BWT算法(Burrows ...

  2. BZOJ 4199&colon; &lbrack;Noi2015&rsqb;品酒大会 &lbrack;后缀数组 带权并查集&rsqb;

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

  3. BZOJ 1692&colon; &lbrack;Usaco2007 Dec&rsqb;队列变换 &lbrack;后缀数组 贪心&rsqb;

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1383  Solved: 582[Submit][St ...

  4. POJ3693 Maximum repetition substring &lbrack;后缀数组 ST表&rsqb;

    Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9458   Acc ...

  5. POJ1743 Musical Theme &lbrack;后缀数组&rsqb;

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27539   Accepted: 9290 De ...

  6. 后缀数组&lpar;suffix array&rpar;详解

    写在前面 在字符串处理当中,后缀树和后缀数组都是非常有力的工具. 其中后缀树大家了解得比较多,关于后缀数组则很少见于国内的资料. 其实后缀数组是后缀树的一个非常精巧的替代品,它比后缀树容易编程实现, ...

  7. 【UOJ &num;35】后缀排序 后缀数组模板

    http://uoj.ac/problem/35 以前做后缀数组的题直接粘模板...现在重新写一下模板 注意用来基数排序的数组一定要开到N. #include<cstdio> #inclu ...

  8. 【BZOJ-2119】股市的预测 后缀数组

    2119: 股市的预测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 334  Solved: 154[Submit][Status][Discuss ...

  9. 【BZOJ-4698】Sandy的卡片 后缀数组

    4698: Sdoi2008 Sandy的卡片 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 140  Solved: 55[Submit][Stat ...

  10. POJ 1743 Musical Theme ——后缀数组

    [题目分析] 其实找最长的不重叠字串是很容易的,后缀数组+二分可以在nlogn的时间内解决. 但是转调是个棘手的事情. 其实只需要o(* ̄▽ ̄*)ブ差分就可以了. 背板题. [代码] #include ...

随机推荐

  1. ORA-39242 错误

    转载: Oracle 11g Release 1 (11.1) Data Pump 技术 http://docs.oracle.com/cd/B28359_01/server.111/b28319/d ...

  2. 修改JSONArray里所有key的值

    下面举一个代码的列子目的是实现如下功能: [{"userId":1,"userName":"plf"},{"userId&quot ...

  3. TYPES、DATA、TYPE、LIKE、CONSTANTS、STATICS、TABLES

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  4. 滑雪 (搜索)(dp)(贪心)

    以每一点为起点找出所有路径,并求出以此点做为起点的最大路径 求出每个点的最大路径后再找出其中最大的值,输出最大值 #include <stdio.h>#include <string ...

  5. 使用aidl绑定远程服务

    一.服务端 1.清单文件,因为要远程调用,所以要配个action <service android:name="com.example.alipayservice.AliPayServ ...

  6. PHP和java比较

    这样从几个方面来看:一.运行机制:Java代码被编译成字节码后,会在虚拟机里由JIT进行二次编译成为本地码,据传言其执行速度可以和C++相媲美,经过我自己测试,用Java实现一个简单的Memcache ...

  7. Python——pyqt5——智能提示(lineEdit&sol;conmbobox)

    一.文本框智能补全 completer = QtWidgets.QCompleter(data) completer.setCompletionMode(QtWidgets.QCompleter.Po ...

  8. 《剑指offer》第五十二题(两个链表的第一个公共结点)

    // 面试题52:两个链表的第一个公共结点 // 题目:输入两个链表,找出它们的第一个公共结点. #include <iostream> #include "List.h&quo ...

  9. spss C&num; 二次开发 学习笔记(二)——Spss以及统计术语解释(IT人眼中的统计术语)

    针对客户需求,需要对一些数据做统计分析.统计分析的第一步,即为数据查询,查找出要统计分析的数据. 查询得出的是一个行列表格的结果集,行.列.表格等这些IT的数据库概念和Spss以及统计中的术语是如何对 ...

  10. 20155330 实验一《Java开发环境的熟悉》(Windows&plus;IDEA)实验报告

    实验知识点 JVM.JRE.JDK的安装位置与区别: 命令行运行javac:java:javac -cp; java -cp: PATH,CLASSPATH,SOURCEPATH的设定方法与应用: 包 ...