Long Long Message

poj2774:http://poj.org/problem?id=2774

题意:

题解:

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 using namespace std;
  5 const int maxn=220010;
  6 char str[maxn];
  7 char temp[maxn];
  8 int wa[maxn],wb[maxn],wv[maxn],wn[maxn],a[maxn],sa[maxn];
  9 int num;
 10 int cmp(int* r,int a,int b,int l)
 11 {
 12     return r[a]==r[b]&&r[a+l]==r[b+l];
 13 }
 14 //n为字符串长度,m为字符的取值范围,r为字符串。后面的j为每次排序时子串的长度
 15 void DA(int* r,int* sa,int n,int m)
 16 {
 17     int i,j,p,*x=wa,*y=wb,*t;
 18     ///对R中长度为1的子串进行基数排序
 19     for(i=0; i<m; i++)wn[i]=0;
 20     for(i=0; i<n; i++)wn[x[i]=r[i]]++;
 21     for(i=1; i<m; i++)wn[i]+=wn[i-1];
 22     for(i=n-1; i>=0; i--)sa[--wn[x[i]]]=i;
 23     for(j=1,p=1; p<n; j*=2,m=p)
 24     {
 25         //利用了上一次基数排序的结果,对待排序的子串的第二关键字进行了一次高效地基数排序
 26         for(p=0,i=n-j; i<n; i++)y[p++]=i;
 27         for(i=0; i<n; i++)if(sa[i]>=j)y[p++]=sa[i]-j;
 28         ///基数排序
 29         for(i=0; i<n; i++)wv[i]=x[y[i]];
 30         for(i=0; i<m; i++)wn[i]=0;
 31         for(i=0; i<n; i++)wn[wv[i]]++;
 32         for(i=1; i<m; i++)wn[i]+=wn[i-1];
 33         for(i=n-1; i>=0; i--)sa[--wn[wv[i]]]=y[i];
 34         ///当p=n的时候,说明所有串都已经排好序了
 35         ///在第一次排序以后,rank数组中的最大值小于p,所以让m=p
 36         for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1; i<n; i++)
 37             x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
 38     }
 39     return;
 40 }
 41 ///后缀数组  计算height数组
 42 /**
 43 height数组的值应该是从height[1]开始的,而且height[1]应该是等于0的。
 44 原因是,+因为我们在字符串后面添加了一个0号字符,所以它必然是最小的
 45 一个后缀。而字符串中的其他字符都应该是大于0的(前面有提到,使用倍
 46 增算法前需要确保这点),所以排名第二的字符串和0号字符的公共前缀
 47 (即height[1])应当为0.在调用calheight函数时,要注意height数组的范
 48 围应该是[1..n]。所以调用时应该是calheight(r,sa,n)
 49 而不是calheight(r,sa,n+1)。*/
 50 int rank[maxn],height[maxn];
 51 void calheight(int* r,int* sa,int n)
 52 {
 53     int i,j,k=0;
 54     for(i=1; i<=n; i++)rank[sa[i]]=i;
 55     for(i=0; i<n; height[rank[i++]]=k)
 56         for(k?k--:0,j=sa[rank[i]-1]; r[i+k]==r[j+k]; k++);
 57     return;
 58 }
 59 int t;
 60 bool judge(int mid){
 61     int counts=0;
 62    for(int i=1;i<=t;i++){
 63        if(height[i]<mid){
 64         counts=0;
 65        }
 66        else{
 67              counts++;
 68             if(counts>=num-1)
 69             return true;
 70           }
 71     }
 72
 73    return false;
 74 }
 75 int flag[maxn];
 76 int main(){
 77     while(~scanf("%s",&str)){
 78         scanf("%s",temp);
 79       memset(flag,0,sizeof(flag));
 80       int len1=strlen(str);
 81       int len2=strlen(temp);
 82        for(int i=0;i<len1;i++){
 83           a[i]=(int)str[i];
 84           flag[i]=1;
 85        }
 86        a[len1]=200;
 87        for(int i=0;i<len2;i++){
 88            a[i+len1+1]=(int)temp[i];
 89            flag[i+len1+1]=2;
 90        }
 91         a[len1+len2+1]=0;
 92         int t=len1+len2+1;
 93         DA(a,sa,t+1,260);
 94         calheight(a,sa,t);
 95         int ans=0;
 96         for(int i=2;i<=t;i++){
 97             if(flag[sa[i]]+flag[sa[i-1]]==3){
 98                 ans=max(ans,height[i]);
 99             }
100
101         }
102         printf("%d\n",ans);
103
104     }
105     return 0;
106 }

Long Long Message,布布扣,bubuko.com

时间: 2024-10-21 23:53:26

Long Long Message的相关文章

Handler和Message详解

Android开发中Handler的经典总结 当应用程序启动时,Android首先会开启一个主线程(也就是UI线程),主线程为管理界面中的UI控件,进行事件分发. 一.Handler的定义: 主要接受子线程发送的数据, 并用此数据配合主线程更新UI. 解释:当应用程序启动时,Android首先会开启一个主线程 (也就是UI线程) , 主线程为管理界面中的UI控件, 进行事件分发, 比如说, 你要是点击一个 Button ,Android会分发事件到Button上,来响应你的操作.  如果此时需要

“intel_rapl: no valid rapl domains” message upon boot blacklist.conf -1

"intel_rapl: no valid rapl domains" message upon boot I install fresh Ubuntu 14. 04 server under ESXI 5.1.0.During my installation I did not get any erorrs or strange things. My problem is when I boot machine at console screen I get messages lik

hdu 4661 Message Passing(树形DP&amp;组合数学)

Message Passing Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1187    Accepted Submission(s): 423 Problem Description There are n people numbered from 1 to n. Each people have a unique mes

HDU 4300 Clairewd’s message

Problem Description Clairewd is a member of FBI. After several years concealing in BUPT, she intercepted some important messages and she was preparing for sending it to ykwd. They had agreed that each letter of these messages would be transfered to a

Long Long Message(后缀数组)

Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 30427   Accepted: 12337 Case Time Limit: 1000MS Description The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days

解决LLDB模式下出现message sent to deallocated instance错误

本文在源文的基础上做整理:http://www.devdiv.com/home.php?mod=space&uid=50901&do=blog&id=50856 Xcode版本7.1 IOS版本9.1 公司之前开发的一个APP,在使用环境中IOS版本升级到9.0以后,某个操作会导致程序闪退. Xcode代码中提示错误: 1 Thread 1: Program received signal:"EXC_BAD_ACCESS" 这样的提示意味着对已经释放的对象发送消

webapi &lt;Message&gt;已拒绝为此请求授权。&lt;/Message&gt;

原有的调用base.OnAuthorization(actionContext); 换成下面这个 /// <summary> /// 鉴权 /// </summary> /// <param name="actionContext"></param> public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContex

[Angular] Scrolling the Message List To the Bottom Automatically Using OnChanges

Let's say the message list can Input (messages) from parent component, and what we want to do is when there are new message, we will scroll message list to the bottom to show the lastest message. import {Component, Input, ViewChild, OnChanges, Simple

Android Message Handling Mechanism

转自:http://solarex.github.io/blog/2015/09/22/android-message-handling-mechanism/ Android is a message driven, message driven several elements: The message says: Message Message queue: MessageQueue The news cycle, remove the message processing for circ

JAVA多线程(九)模式-Thread Per Message

Thread Per Message 请求的频率高于处理速度,由线程池平衡请求速度和处理速度. 适用环境 不需要返回数据,处理顺序无关的场景. 样例 每一次请求都由线程池中一个线程处理. 服务端 package ThreadPerMessage; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Host{ private ExecutorService