BestCoder Round #49 HDU5340 Three Palindromes Manacher算法

来源:互联网 发布:f450破解用户数软件 编辑:程序博客网 时间:2024/06/05 10:44



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
2abcabaadada
 

Sample Output
YesNo
 

Source
BestCoder Round #49 ($)


出题人:

对原串前缀和后缀作一个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算法:








0 0
原创粉丝点击