BestCoder Round #49 HDU5340 Three Palindromes Manacher算法

49 篇文章 0 订阅
35 篇文章 0 订阅



Three Palindromes

                                                         Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
                                                                                   Total Submission(s): 1402    Accepted Submission(s): 474


Problem Description
Can we divided a given string S into three nonempty palindromes?
 

Input
First line contains a single integer  T20  which denotes the number of test cases.

For each test case , there is an single line contains a string S which only consist of lowercase English letters. 1|s|20000
 

Output
For each case, output the "Yes" or "No" in a single line.
 

Sample Input
  
  
2 abc abaadada
 

Sample Output
  
  
Yes No
 

Source


出题人:

对原串前缀和后缀作一个01标记pre[i],suf[i]表示1-i和i-n否能形成回文。

记以i为中心的回文半径为r(i)。这些都可以O(N)时间内求出。

也可以使用Hash+二分等方法O(NlogN)内求出。

我们考虑中间一个回文串的位置,不妨设它是奇数长度(偶数类似)。

那么问题变成了求一个i和d使得1<=d<=r(i)且pre[i-d]和suf[i+d]为真。

枚举i,实际上就是问pre[i-r(i)..i-1]和suf[i+1..i+r(i)]取反后 

这两段有没有一个位置两者均为1,也就是and后不为0,

暴力压位即可。总时间复杂度为O(N2/32)

O(N^2/32)


我用的Manacher算法


#include
   
   
    
    
#include
    
    
     
     
#include
     
     
      
      
#include
      
      
       
       
#include
       
       
         #define min(a,b) a 
        
          b?a:b const int maxn=1000010; char str[maxn]; ///原字符串 char tmp[maxn<<1]; ///转换后的字符串 int Len[maxn<<1]; ///转换原始串 int top[maxn<<1],tail[maxn<<1]; int INIT(char *st) ///原字符串 { int i,len=strlen(st); tmp[0]='@'; ///字符串开头增加一个特殊字符,防止越界 for(i=1; i<=len<<1; i+=2) { tmp[i]='#'; tmp[i+1]=st[i>>1]; } tmp[len<<1+1]='#'; tmp[len<<1+2]='$'; ///字符串结尾加一个字符,防止越界 tmp[len<<1+3]=0; return len<<1+1; ///返回转换字符串的长度 } ///Manacher算法计算过程 int MANACHER(char *st,int len) ///转换字符串(st[])和长度(len) { int mx=0; ///mx即为当前计算回文串最右边字符的最大值 int po=0; ///po即当前Len[i]最大的位置i int ans=0; ///当前最长回文串长度 for(int i=1; i<=len; i++) { if(mx>i) ///在Len[j]和mx-i中取个小 { Len[i]=min(mx-i,Len[2*po-i]); } else Len[i]=1; ///如果i>=mx,要从头开始匹配 while(st[i-Len[i]]==st[i+Len[i]]) { Len[i]++; } if(Len[i]+i>mx) ///若新计算的回文串右端点位置大于mx,要更新po和mx的值 { mx=Len[i]+i; po=i; } ans=max(ans,Len[i]); } return ans-1; ///返回Len[i]中的最大值-1即为原串的最长回文子串额长度 } int main() { int t,s,l,flag,i,j,ll,rr,mid; scanf("%d",&t); while(t--) { flag=0; scanf("%s",str); l=strlen(str); if(l<=3) { if(l==3) flag=1; } else { s=MANACHER(tmp,INIT(str)); if(l-s==2||l==s) flag=1; else { l*=2; l+=2; int k1=0,k2=0; for(i=2; i 
         
           =0; j--) { ll=top[i]+Len[top[i]]; rr=tail[j]-Len[tail[j]]; if(ll>rr) break; mid=(ll+rr)>>1; if(Len[mid]-1>=mid-ll) { flag=1; break; } } if(flag) break; } } } if(flag) puts("Yes"); else puts("No"); } return 0; } 
          
         
       
      
      
     
     
    
    
   
   






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值