zoukankan      html  css  js  c++  java
  • URAL 题目1297. Palindrome(后缀数组+RMQ求最长回文子串)

    1297. Palindrome

    Time limit: 1.0 second
    Memory limit: 64 MB
    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.

    Sample

    input output
    ThesampletextthatcouldbereadedthesameinbothordersArozaupalanalapuazorA
    
    ArozaupalanalapuazorA
    
    Problem Author: Eugene Krokhalev
    Problem Source: IX Open Collegiate Programming Contest of the High School Pupils (13.03.2004)
    Difficulty: 96    Printable version    Submit solution    Discussion (62)

    My submissions    All submissions (19930)    All accepted submissions (6214)    Solutions rating (4095)

    ac代码

    首先肯定是要把原字符的逆序串接到原字符串的后面。然后便是从0~~len扫描这个字符串,如果第i个位置是一个回文的中心。然后求出i个i相应位置的lca的最大值就可以。每次枚举时要分奇偶两种情况考虑。


    #include<stdio.h>       
    #include<string.h>       
    #include<algorithm>       
    #include<iostream>      
    #define min(a,b) (a>b?b:a)   
    #define max(a,b) (a>b?a:b)    
    using namespace std;      
    char str[3030];    
    int sa[3030],Rank[3030],rank2[3030],height[3030],c[3030],*x,*y,s[3030];  
    int n;  
    void cmp(int n,int sz)  
    {  
        int i;  
        memset(c,0,sizeof(c));  
        for(i=0;i<n;i++)  
            c[x[y[i]]]++;  
        for(i=1;i<sz;i++)  
            c[i]+=c[i-1];  
        for(i=n-1;i>=0;i--)  
            sa[--c[x[y[i]]]]=y[i];  
    }  
    void build_sa(int *s,int n,int sz)  
    {  
        x=Rank,y=rank2;  
        int i,j;  
        for(i=0;i<n;i++)  
            x[i]=s[i],y[i]=i;  
        cmp(n,sz);  
        int len;  
        for(len=1;len<n;len<<=1)  
        {  
            int yid=0;  
            for(i=n-len;i<n;i++)  
            {  
                y[yid++]=i;  
            }  
            for(i=0;i<n;i++)  
                if(sa[i]>=len)  
                    y[yid++]=sa[i]-len;  
                cmp(n,sz);  
            swap(x,y);  
            x[sa[0]]=yid=0;  
            for(i=1;i<n;i++)  
            {  
                if(y[sa[i-1]]==y[sa[i]]&&sa[i-1]+len<n&&sa[i]+len<n&&y[sa[i-1]+len]==y[sa[i]+len])  
                    x[sa[i]]=yid;  
                else  
                    x[sa[i]]=++yid;  
            }  
            sz=yid+1;  
            if(sz>=n)  
                break;  
        }  
        for(i=0;i<n;i++)  
            Rank[i]=x[i];  
    }  
    void getHeight(int *s,int n)  
    {  
        int k=0;  
        for(int i=0;i<n;i++)  
        {  
            if(Rank[i]==0)  
                continue;  
            k=max(0,k-1);  
            int j=sa[Rank[i]-1];  
            while(s[i+k]==s[j+k])  
                k++;  
            height[Rank[i]]=k;  
        }  
    }
    int minv[3010][20],lg[3030];  
    void init_lg()
    {
    	int i;
    	lg[1]=0;
    	for(i=2;i<2020;i++)
    	{
    		lg[i]=lg[i>>1]+1;
    	}
    }
    void init_RMQ(int n)
    {
    	int i,j,k;
    	for(i=1;i<=n;i++)
    	{
    		minv[i][0]=height[i];
    	}
    	for(j=1;j<=lg[n];j++)  
        {  
            for(k=0;k+(1<<j)-1<=n;k++)  
            {  
                minv[k][j]=min(minv[k][j-1],minv[k+(1<<(j-1))][j-1]);   
            }  
        }
    }
    int lcp(int l,int r)
    {
    	l=Rank[l];
    	r=Rank[r];
    	if(l>r)
    		swap(l,r);
    	l++;
    	int k=lg[r-l+1];
    	return min(minv[l][k],minv[r-(1<<k)+1][k]);  
    }
    int main()
    {
    	while(scanf("%s",str)!=EOF)
    	{
    		int n=0,i;
    		int len=strlen(str);
    		for(i=0;i<len;i++)
    		{
    			s[n++]=str[i];
    		}
    		s[n++]=127;
    		for(i=len-1;i>=0;i--)
    			s[n++]=str[i];
    		s[n]=0;
    		build_sa(s,n+1,128);
    		getHeight(s,n);
    		init_lg();
    		init_RMQ(n);
    		int ans=-1,st;
    		for(i=0;i<len;i++)
    		{
    			int temp=lcp(i,n-i-1);
    			if(temp*2-1>ans)
    			{
    				ans=temp*2-1;
    				st=i-temp+1;
    			}
    			temp=lcp(i,n-i);
    			if(temp*2>ans)
    			{
    				ans=temp*2;
    				st=i-temp;
    			}
    		}
    	//	printf("%d %d
    ",ans,st);
    		if(ans==1)
    			printf("%c
    ",str[0]);
    		else
    		{
    			for(i=0;i<ans;i++)
    			{
    				printf("%c",str[i+st]);
    			}
    			printf("
    ");
    		}
    	}
    }


  • 相关阅读:
    Win7 SP1 安装SQL Server 2012时提示“此计算机上的操作系统不符合 SQL Server 2012的最低要求”
    ajax jsonp跨域
    Caused by: Unable to locate parent package [json-package] for [class com.you.action.ColumnAction]
    PHP MVC自己主动RBAC自己主动生成的访问路由
    Service与Activity与交流AIDL
    SVN常见错误两项纪录
    EL表达式语言
    oracle11g ASM(修复损坏的磁盘组头asm修复2)
    如何使用iOS 8 指纹识别,代码、示例
    EXCEL Pivot table manipulate
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/7339030.html
Copyright © 2011-2022 走看看