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即可。

代码如下:

  1. #include<cstdio>
  2. #include<cstdlib>
  3. #include<cstring>
  4. #include<iostream>
  5. #include<algorithm>
  6. #include<queue>
  7. using namespace std;
  8. #define Maxl 100010
  9. #define INF 0xfffffff
  10.  
  11. int l,len;
  12. char s[Maxl];
  13. int c[Maxl],cl;
  14.  
  15. int mymin(int x,int y) {return x<y?x:y;}
  16. int mymax(int x,int y) {return x>y?x:y;}
  17.  
  18. void init()
  19. {
  20. len=strlen(s);
  21. cl=;
  22. for(int i=;i<len;i++) if(s[i]!=' ')
  23. c[++cl]=s[i]-'A'+;
  24. }
  25.  
  26. int sa[Maxl],rk[Maxl],y[Maxl],wr[Maxl],Rs[Maxl];
  27. void get_sa(int m)
  28. {
  29. memcpy(rk,c,sizeof(rk));
  30. for(int i=;i<=m;i++) Rs[i]=;
  31. for(int i=;i<=cl;i++) Rs[rk[i]]++;
  32. for(int i=;i<=m;i++) Rs[i]+=Rs[i-];
  33. for(int i=cl;i>=;i--) sa[Rs[rk[i]]--]=i;
  34.  
  35. int ln=,p=;
  36. while(p<cl)
  37. {
  38. int k=;
  39. for(int i=cl-ln+;i<=cl;i++) y[++k]=i;
  40. for(int i=;i<=cl;i++) if(sa[i]>ln) y[++k]=sa[i]-ln;
  41. for(int i=;i<=cl;i++) wr[i]=rk[y[i]];
  42.  
  43. for(int i=;i<=m;i++) Rs[i]=;
  44. for(int i=;i<=cl;i++) Rs[wr[i]]++;
  45. for(int i=;i<=m;i++) Rs[i]+=Rs[i-];
  46. for(int i=cl;i>=;i--) sa[Rs[wr[i]]--]=y[i];
  47.  
  48. for(int i=;i<=cl;i++) wr[i]=rk[i];
  49. for(int i=cl+;i<=cl+ln;i++) wr[i]=;
  50. p=,rk[sa[]]=;
  51. for(int i=;i<=cl;i++)
  52. {
  53. if(wr[sa[i]]!=wr[sa[i-]]||wr[sa[i]+ln]!=wr[sa[i-]+ln]) p++;
  54. rk[sa[i]]=p;
  55. }
  56. ln*=,m=p;
  57. }
  58. sa[]=rk[]=;
  59. }
  60.  
  61. int height[Maxl];
  62. void get_he()
  63. {
  64. int k=;
  65. for(int i=;i<=cl;i++) if(rk[i]!=)
  66. {
  67. int j=sa[rk[i]-];
  68. if(k) k--;
  69. while(c[i+k]==c[j+k]&&i+k<=cl&&j+k<=cl) k++;
  70. height[rk[i]]=k;
  71. }
  72. }
  73.  
  74. void ffind()
  75. {
  76. for(int i=;i<=cl;i++)//枚举长度i
  77. {
  78. int cnt=,ans=;
  79. for(int j=;j<=cl;j++)
  80. {
  81. cnt++;
  82. if(height[j+]<i||j==cl)//是一组的结束
  83. {
  84. if(cnt!=) ans=mymax(ans,cnt);
  85. cnt=;
  86. }
  87. }
  88. if(ans<=) break;
  89. printf("%d\n",ans);
  90. }
  91. }
  92.  
  93. int main()
  94. {
  95. bool ok=;
  96. while(gets(s))
  97. {
  98. if(ok) printf("\n");
  99. ok=;
  100. init();
  101. get_sa();
  102. get_he();
  103. ffind();
  104. }
  105. return ;
  106. }

[UVA11855]

2016-07-19 16:31:07

【POJ11855】 Buzzwords (后缀数组)的更多相关文章

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

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

  2. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

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

  3. BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]

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

  4. POJ3693 Maximum repetition substring [后缀数组 ST表]

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

  5. POJ1743 Musical Theme [后缀数组]

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

  6. 后缀数组(suffix array)详解

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

  7. 【UOJ #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. c语言输入输出

    一 #include "stdio.h"int main(){ FILE *fp; int ninzu=0; char name[100]; double hsum=0.0; do ...

  2. ObjectInputStream ObjectOutStream

    1:不能多次read 2:read 与write操作必须一对一

  3. ThinkPHP的数据库访问的简单操作

    传统的sql与ThinkPHP中的sql相比较   以user表为例 $user=M('user'); 1: SELECT * FROM user----------$user->select( ...

  4. Android常用错误解决汇总

    一.No active compatible AVD's or devices found. Relaunch this configuration after connecting a device ...

  5. jsoup:解析HTML用法小结

    1.解析方式 (1)从字符串解析 ? 1 2 3 String html = "<html><head><title>First parse</ti ...

  6. Java23种设计模式之单例模式

    一.单例模式简介 单例模式是Java设计模式中常见的一种模式.主要分为懒汉式单例.饿汉式单例.登记式单例: 单例模式的特点:  1.单例类只能有一个实例:  2.单例类必须自己创建自己的唯一的实例: ...

  7. C# 匿名表达式(Lambda表达式)

    匿名表达式 这次来说说Lambda表达式吧,反正也简单,我也不像其他高手那样强调来强调去,只讲一下方法: 准备条件如下: 第一,匿名表达式必须存在与之对应的委托. 只要存在相对应的委托就可以了.接下来 ...

  8. C++类型转换总结 转

    一.前言: C风格的强制类型转换(Type Cast)很简单,不管什么类型的转换统统是: TYPE b = (TYPE)a. C++风格的类型转换提供了4种类型转换操作符来应对不同场合的应用. con ...

  9. Oracel用rownum实现真分页

    因为oracle的rownum是一个伪列,使用的时候如果要用必须查询出来显示的标记例如本sql中标记为 num. 值得一提的是最内层的这个查询sql:“select a.集团规范编码...”本身有一个 ...

  10. js隔行变色

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...