spoj 220 Relevant Phrases of Annihilation

本文介绍了一种用于解析重复出现于多个敌军加密消息中最长非重叠片段的算法。该算法首先将所有字符串连接并用特殊字符隔开,然后通过后缀数组和二分查找确定满足条件的最长字符串。此方法适用于寻找至少两次独立出现的关键信息。

You are the King of Byteland. Your agents have just intercepted a batch of encrypted enemy messages concerning the date of the planned attack on your island. You immedietaly send for the Bytelandian Cryptographer, but he is currently busy eating popcorn and claims that he may only decrypt the most important part of the text (since the rest would be a waste of his time). You decide to select the fragment of the text which the enemy has strongly emphasised, evidently regarding it as the most important. So, you are looking for a fragment of text which appears in all the messages disjointly at least twice. Since you are not overfond of the cryptographer, try to make this fragment as long as possible.

Input

The first line of input contains a single positive integer t<=10, the number of test cases. t test cases follow. Each test case begins with integer n (n<=10), the number of messages. The next n lines contain the messages, consisting only of between 2 and 10000 characters 'a'-'z', possibly with some additional trailing white space which should be ignored.

Output

For each test case output the length of longest string which appears disjointly at least twice in all of the messages.

Example

Input:
1
4
abbabba
dabddkababa
bacaba
baba

Output:
2

(in the example above, the longest substring which fulfills the requirements is 'ba')

 

算法分析:
做法和上题大同小异,也是先将 n 个字符串连起来,中间用不相同的且没有
出现在字符串中的字符隔开,求后缀数组。然后二分答案,再将后缀分组。判断
的时候,要看是否有一组后缀在每个原来的字符串中至少出现两次,并且在每个
原来的字符串中,后缀的起始位置的最大值与最小值之差是否不小于当前答案
(判断能否做到不重叠,如果题目中没有不重叠的要求,那么不用做此判断)。
这个做法的时间复杂度为 O(nlogn)。

 

 1 #include<bits/stdc++.h>
 2 using namespace std;  
 3 int const N=200000+1000; 
 4 int const inf=10000000;   
 5 int wa[N<<1],wb[N<<1],wv[N],num[N],rk[N],h[N],sa[N],r[N],id[N],vx[11],vy[11],n;    
 6 char s1[N]; 
 7 int cmp(int *r,int x,int y,int z){
 8     return r[x]==r[y] && r[x+z]==r[y+z];  
 9 }  
10 void build_sa(int *r,int *sa,int n,int m){
11     int i,j,p,*x=wa,*y=wb;  
12     for(i=0;i<m;i++) num[i]=0;  
13     for(i=0;i<n;i++) num[x[i]=r[i]]++;  
14     for(i=1;i<m;i++) num[i]+=num[i-1];  
15     for(i=n-1;i>=0;i--)  sa[--num[x[i]]]=i;  
16     for(j=1,p=1;p<n;j<<=1,m=p){
17         for(p=0,i=n-j;i<n;i++) y[p++]=i;  
18         for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;  
19         for(i=0;i<m;i++) num[i]=0;  
20         for(i=0;i<n;i++) num[wv[i]=x[y[i]]]++;  
21         for(i=1;i<m;i++) num[i]+=num[i-1];  
22         for(i=n-1;i>=0;i--) sa[--num[wv[i]]]=y[i];  
23         swap(x,y);  
24         for(i=1,p=1,x[sa[0]]=0;i<n;i++)  
25             x[sa[i]]=cmp(y,sa[i],sa[i-1],j)? p-1:p++; 
26     }
27     for(i=0;i<n;i++)  rk[i]=x[i]; 
28 }  
29 void build_h(int *r,int *sa,int n){
30     int k=0; 
31     for(int i=0;i<n;i++){
32         if(k) k--;  
33         int j=sa[rk[i]-1];  
34         while (r[i+k]==r[j+k]) k++;  
35         h[rk[i]]=k;  
36     }
37 } 
38 int check(int mid,int len ){ 
39     for(int i=1;i<=n;i++)  vx[i]=inf,vy[i]=-1;    
40     for(int i=1;i<=len;i++){
41         if(h[i]>=mid){
42             int a=id[sa[i]];  
43             int b=id[sa[i-1]];  
44             vx[a]=min(vx[a],sa[i]);  
45             vx[b]=min(vx[b],sa[i-1]);  
46             vy[a]=max(vy[a],sa[i]);  
47             vy[b]=max(vy[b],sa[i-1]); 
48             int check=0;  
49             for(int j=1;j<=n;j++){
50                 if(vy[j]-vx[j]<mid) {
51                     check=1;  break;  
52                 }
53             }
54             if(!check)  return  1; 
55         }else {
56             for(int j=1;j<=n;j++) vx[j]=inf,vy[j]=-1;    
57         }
58     }
59     return 0; 
60 }
61 int main(){
62     int cas;  
63     scanf("%d",&cas);  
64     while (cas--){
65         memset(r,0,sizeof(r)); 
66         memset(id,0,sizeof(id));      
67         int sum=122;  
68         scanf("%d",&n);
69         int len=0;  
70         for(int j=1;j<=n;j++) {
71             scanf("%s",s1);   
72             for(int i=0;s1[i];i++)  
73                 r[len++]=s1[i],id[len-1]=j;  
74             r[len++]=++sum;  
75         } 
76         build_sa(r,sa,len+1,140);  
77         build_h(r,sa,len);  
78         int l=0,r=len,ans=0;  
79         while (l<=r){
80             int mid=(l+r)/2;  
81             if(check(mid,len)) {
82                 ans=mid;  
83                 l=mid+1; 
84             }else r=mid-1;  
85         } 
86         printf("%d\n",ans);       
87     }
88     return 0; 
89 }
View Code

 

转载于:https://www.cnblogs.com/ZJXXCN/p/11003720.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值