zoukankan      html  css  js  c++  java
  • 1297. Palindrome ural1297(后缀数组)

    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

    inputoutput
    ThesampletextthatcouldbereadedthesameinbothordersArozaupalanalapuazorA
    
    ArozaupalanalapuazorA
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <math.h>
     4 #include <string.h>
     5 using namespace std;
     6 #define N 2005
     7 char a[N],bb[N];
     8 int c[N],d[N],e[N],sa[N],height[N],n,b[N],m,dp[N][12];
     9 int cmp(int *r,int a,int b,int l)
    10 {
    11     return r[a]==r[b]&&r[a+l]==r[b+l];
    12 }
    13 void da()
    14 {
    15     int i,j,p,*x=c,*y=d,*t;
    16     memset(b,0,sizeof(b));
    17     for(i=0; i<n; i++)b[x[i]=a[i]]++;
    18     for(i=1; i<m; i++)b[i]+=b[i-1];
    19     for(i=n-1; i>=0; i--)sa[--b[x[i]]]=i;
    20     for(j=1,p=1; p<n; j*=2,m=p)
    21     {
    22         for(p=0,i=n-j; i<n; i++)y[p++]=i;
    23         for(i=0; i<n; i++)if(sa[i]>=j)y[p++]=sa[i]-j;
    24         for(i=0; i<n; i++)e[i]=x[y[i]];
    25         for(i=0; i<m; i++)b[i]=0;
    26         for(i=0; i<n; i++)b[e[i]]++;
    27         for(i=1; i<m; i++)b[i]+=b[i-1];
    28         for(i=n-1; i>=0; i--)sa[--b[e[i]]]=y[i];
    29         for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1; i<n; i++)
    30             x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
    31     }
    32 }
    33 void callheight()
    34 {
    35     int i,j,k=0;
    36     b[0]=0;
    37     for(i=1; i<n; i++)b[sa[i]]=i;
    38     for(i=0; i<n-1; height[b[i++]]=k)
    39         for(k?k--:0,j=sa[b[i]-1]; a[i+k]==a[j+k]; k++);
    40 }
    41 int fun(int i,int j)
    42 {
    43     i=b[i];
    44     j=b[j];
    45     if(i>j)swap(i,j);
    46     i++;
    47     int k=(int)(log(j-i+1.0)/log (2.0));
    48     return min(dp[i][k],dp[j-(1<<k)+1][k]);
    49 }
    50 void initrmq()
    51 {
    52     int i,j;
    53     memset(dp,0,sizeof(dp));
    54     for(i=0; i<=n; i++)
    55         dp[i][0]=height[i];
    56     for(j=1; (1<<j)<=n; j++)
    57         for(i=0; i+(1<<j)<=n; i++)
    58             dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
    59 }
    60 int main()
    61 {
    62     m=200;
    63     scanf("%s",a);
    64     int size=n=strlen(a);
    65     strcpy(bb,a);
    66     a[n]='#';
    67     strrev(bb);
    68     strcat(a,bb);
    69     n=strlen(a);
    70     a[n++]='';
    71     da();
    72     callheight();
    73     n--;
    74     initrmq();
    75     int maxa=0,maxi=0;
    76     for(int i=0; i<size; i++)
    77     {
    78         int tt=fun(i,n-i);
    79         if((tt<<1)>maxa)
    80         {
    81             maxa=tt<<1;
    82             maxi=i-tt;
    83         }
    84         tt=fun(i,n-i-1);
    85         if((tt<<1)-1>maxa)
    86         {
    87             maxa=(tt<<1)-1;
    88             maxi=i-(--tt);
    89         }
    90     }
    91     for(int i=0; i<maxa; i++)
    92         putchar(a[i+maxi]);
    93    putchar('
    ');
    94 }
    View Code
  • 相关阅读:
    Java高级架构师(一)第04节:Git基本原理和安装配置使用
    发光边框
    单位px 转换成 rem
    web app 自适应 弹性布局之rem
    移动端UC /QQ 浏览器的部分私有Meta 属性
    常用<meta>标签
    移动端<head>头部 常用<meta>标签
    移动平台对 META 标签的定义
    减去border边框
    伪类共用样式缩写形式
  • 原文地址:https://www.cnblogs.com/ERKE/p/3596618.html
Copyright © 2011-2022 走看看