URAL - 1297 Palindrome【Manacher或后缀数组】【最长回文子串】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/niiick/article/details/84895174

Time limit 1000 ms
Memory limit 65536 kB

The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing «Robots Unlimited» has infiltrated into “U.S. Robotics”. «U.S. Robots» security service would have already started an undercover operation to establish the agent’s identity, but, fortunately, the letter describes communication channel the agent uses. He will publish articles containing stolen data to the “Solaris” almanac. Obviously, he will obfuscate the data, so “Robots Unlimited” will have to use a special descrambler (“Robots Unlimited” part number NPRx8086, specifications are kept secret).
Having read the letter, the “U.S. Robots” president recalled having hired the “Robots Unlimited” ex-employee John Pupkin. President knows he can trust John, because John is still angry at being mistreated by “Robots Unlimited”. Unfortunately, he was fired just before his team has finished work on the NPRx8086 design.
So, the president has assigned the task of agent’s message interception to John. At first, John felt rather embarrassed, because revealing the hidden message isn’t any easier than finding a needle in a haystack. However, after he struggled the problem for a while, he remembered that the design of NPRx8086 was still incomplete. “Robots Unlimited” fired John when he was working on a specific module, the text direction detector. Nobody else could finish that module, so the descrambler will choose the text scanning direction at random. To ensure the correct descrambling of the message by NPRx8086, agent must encode the information in such a way that the resulting secret message reads the same both forwards and backwards.
In addition, it is reasonable to assume that the agent will be sending a very long message, so John has simply to find the longest message satisfying the mentioned property.
Your task is to help John Pupkin by writing a program to find the secret message in the text of a given article. As NPRx8086 ignores white spaces and punctuation marks, John will remove them from the text before feeding it into the program.

Input

The input consists of a single line, which contains a string of Latin alphabet letters (no other characters will appear in the string). String length will not exceed 1000 characters.

Output

The longest substring with mentioned property. If there are several such strings you should output the first of them.


2009集训队论文《后缀数组——处理字符串的有力工具》的例题
其实Manacher就可以 O ( n ) O(n) 解决
但我们就是闲着没事要用后缀数组


后缀数组求解

尝试枚举回文串的中心
并分别讨论回文串长度为奇偶的两种情况

假如枚举的位置为 m i d mid
那么以 m i d mid 为中心的最长回文子串的 对称长度为
m i d + 1 mid+1 开头的后缀 m i d 1 mid-1 结尾的前缀lcp(回文串长度为奇数)
m i d mid 开头的后缀 m i d 1 mid-1 结尾的前缀lcp(回文串长度为偶数)

要匹配一个前缀的后缀与一个后缀的前缀的lcp很麻烦,尝试转化一下
我们把原字符串翻转接在原字符串后面(注意中间隔一个特殊字符)
这样就可以转化为求两个后缀的lcp
即对于枚举的 m i d mid ,我们找到 m i d 1 mid-1 结尾的前缀反转后对应的位置
假设在拼接后的字符串中位置为 k k
我们只需要求后缀 m i d mid 与后缀 k k 的lcp即可

而对于任意两个后缀 后缀 x x 与后缀 y y 的lcp(假设后缀 x x 排名小于后缀 y y )
我们只需要求出 M i n i = r a k [ x ] + 1 r a k [ y ] ( h e i g h t [ i ] ) Min_{i=rak[x]+1}^{rak[y]}(height[i]) 即可
这里用st表维护就可以做到近似 O ( 1 ) O(1) 查询

最后总时间复杂度为 O ( n ) O(n)

#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
typedef double dd;

int read()
{
    int f=1,x=0;
    char ss=getchar();
    while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
    while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
    return f*x;
}

const int maxn=20010;
int n,m,len;
int a[maxn];
int rak[maxn],sa[maxn],tp[maxn],tax[maxn];
char ss[maxn];
int height[maxn];
int mi[maxn][20],ans,st;

void rsort()
{
    for(int i=0;i<=m;++i) tax[i]=0;
    for(int i=1;i<=n;++i) tax[rak[i]]++;
    for(int i=1;i<=m;++i) tax[i]+=tax[i-1];
    for(int i=n;i>=1;--i) sa[tax[rak[tp[i]]]--]=tp[i];
}

void ssort()
{
    m=2010;
    for(int i=1;i<=n;++i)
    rak[i]=a[i],tp[i]=i;
    
    rsort();
    for(int k=1;k<=n;k<<=1)
    {
        int p=0;
        for(int i=n-k+1;i<=n;++i) tp[++p]=i;
        for(int i=1;i<=n;++i) if(sa[i]>k) tp[++p]=sa[i]-k;
        
        rsort();
        swap(rak,tp);
        rak[sa[1]]=p=1;
        for(int i=2;i<=n;++i)
        rak[sa[i]]=(tp[sa[i]]==tp[sa[i-1]]&&tp[sa[i]+k]==tp[sa[i-1]+k])?p:++p;
        if(p>=n) break;
        m=p;
    }
}

void getH()
{
	int k=0;
	for(int i=1;i<=n;++i)
	{
		if(k) --k;
		int j=sa[rak[i]-1];
		while(a[i+k]==a[j+k]) k++;
		height[rak[i]]=k;
	}
}

void RMQ()
{
	for(int i=1;i<=n;++i) mi[i][0]=height[i];
	for(int j=1;(1<<j)<=n;j++)
    for(int i=1;i+(1<<j)-1<=n;i++)
	mi[i][j]=min(mi[i][j-1],mi[i+(1<<j-1)][j-1]);
}

int qmin(int x,int y)
{
	int ll=min(rak[x],rak[y])+1,rr=max(rak[x],rak[y]);
	int k=0;
	while((1<<k+1)<=rr-ll+1) k++;
	return min(mi[ll][k],mi[rr-(1<<k)+1][k]);
}

int main()
{
    scanf("%s",&ss); 
	len=strlen(ss); ss[len]='#';
    for(int i=len+1;i<=len*2+1;++i) ss[i]=ss[len*2-i];
    n=strlen(ss);
    
    for(int i=0;i<n;++i) a[i+1]=ss[i];
    ssort(); getH();
    
    RMQ();
    for(int i=1;i<=len;++i)
    {
		int tp=0;
    	tp=qmin(i+1,len*2-i+3);//(len-(i-1)+1)+len+1
    	if((tp<<1|1)>ans) ans=tp<<1|1,st=i-tp;//长度为奇数
    	
    	tp=qmin(i,len*2-i+3);
    	if((tp<<1)>ans) ans=tp<<1,st=i-tp;//长度为偶数
	}
    
	for(int i=st;i<=st+ans-1;++i) cout<<ss[i-1];
    //printf("%d",ans);
    return 0;
}

Manacher求解

Manacher的裸题,常数还比后缀数组小
在记录扩展回文串长度的rl数组上找最大值即可

#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
typedef double dd;

int read()
{
    int f=1,x=0;
    char ss=getchar();
    while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
    while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
    return f*x;
}

const int maxn=20010;
int n;
char ss[maxn],a[maxn];
int rl[maxn];
int ans=1,st;

void manacher()
{
    int mr=0,mid;
    for(int i=1;i<n;++i)
    {
        if(i<mr) rl[i]=min(rl[(mid<<1)-i],rl[mid]+mid-i);
        else rl[i]=1;
        while(ss[i+rl[i]]==ss[i-rl[i]])
        {
            if(i+rl[i]>mr) mr=i+rl[i],mid=i;
            ++rl[i];
        }
    }
}

int main()
{
    scanf("%s",&a);
    n=strlen(a);
    ss[0]=ss[1]='#';
    for(int i=0;i<n;++i)
    {
        ss[(i<<1)+2]=a[i];
        ss[(i<<1)+3]='#';
    }
    n=(n<<1)+2; ss[n]=0;
    
    manacher();
    for(int i=0;i<n;++i)
    if(ans<rl[i])
	ans=rl[i],st=i;
	
    int k=st-ans+2;
	for(int i=1;i<=ans-1;++i){ cout<<ss[k]; k+=2;}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/niiick/article/details/84895174
今日推荐