• Manacher Ural 1297 Palindrome


    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 <cstring>
     3 #include <cstdio>
     4 using namespace std;
     5 const int maxn=100010;
     6 char s[maxn],S[maxn];
     7 int maxl[maxn],len;
     8 int main()
     9 {
    10     int l=0;
    11     scanf("%s",s);
    12     len=strlen(s);
    13     S[l++]='$';
    14     S[l++]='&';
    15     for(int i=0;i<len;i++)
    16         S[l++]=s[i],S[l++]='&';
    17     S[l]=0;
    18     int id=0,mp=0;
    19     for(int i=1;i<l;i++){
    20         maxl[i]=mp>i?min(mp-i,maxl[id*2-i]):1;
    21         while(S[i+maxl[i]]==S[i-maxl[i]])maxl[i]++;
    22         if(maxl[i]+i>mp){
    23             mp=maxl[i]+i;
    24             id=i;
    25         }
    26     }    
    27     int ans=0;
    28     for(int i=1;i<=l;i++){
    29         if(maxl[i]>ans)
    30             ans=maxl[i],id=i;
    31     }
    32     for(int i=id-ans+1;i<id+ans;i++)
    33         if(S[i]!='&')
    34             printf("%c",S[i]);
    35     printf("
    ");    
    36 }

      还有后缀数组的解法

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 using namespace std;
     5 const int maxn=5010;
     6 char s[maxn];
     7 int r[maxn],Wa[maxn],Wb[maxn],Wv[maxn],Ws[maxn];
     8 int sa[maxn],rank[maxn],lcp[maxn],len;
     9 bool cmp(int *p,int a,int b,int l){
    10     return p[a]==p[b]&&p[a+l]==p[b+l];
    11 }
    12 void DA(int n,int m){
    13     int i,j,p,*x=Wa,*y=Wb,*t;
    14     for(i=0;i<m;i++)Ws[i]=0;
    15     for(i=0;i<n;i++)++Ws[x[i]=r[i]];
    16     for(i=1;i<m;i++)Ws[i]+=Ws[i-1];
    17     for(i=n-1;i>=0;i--)sa[--Ws[x[i]]]=i;
    18     
    19     for(j=1,p=1;p<n;m=p,j<<=1){
    20         for(i=n-j,p=0;i<n;i++)
    21             y[p++]=i;
    22         for(i=0;i<n;i++)
    23             if(sa[i]>=j)
    24                 y[p++]=sa[i]-j;    
    25         for(i=0;i<m;i++)Ws[i]=0;
    26         for(i=0;i<n;i++)++Ws[Wv[i]=x[y[i]]];
    27         for(i=1;i<m;i++)Ws[i]+=Ws[i-1];
    28         for(i=n-1;i>=0;i--)sa[--Ws[Wv[i]]]=y[i];
    29         for(t=x,x=y,y=t,x[sa[0]]=0,p=1,i=1;i<n;i++)
    30             x[sa[i]]=cmp(y,sa[i],sa[i-1],j)?p-1:p++;
    31     }
    32 }
    33 
    34 void Lcp(int n){
    35     int i,j,k=0;
    36     for(i=1;i<=n;i++)
    37         rank[sa[i]]=i;
    38     for(i=0;i<n;lcp[rank[i++]]=k)
    39         for(k?--k:k,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);    
    40 }
    41 
    42 int mm[maxn],Min[maxn][20];
    43 
    44 void Make_ST(int n){
    45     mm[0]=-1;
    46     for(int i=1;i<=n;i++){
    47         mm[i]=(i&(i-1))?mm[i-1]:mm[i-1]+1;
    48         Min[i][0]=lcp[i];
    49     }
    50     for(int k=1;k<=mm[n];k++)
    51         for(int i=1;i+(1<<k)-1<=n;i++)
    52             Min[i][k]=min(Min[i][k-1],Min[i+(1<<(k-1))][k-1]);
    53     return;        
    54 }
    55 
    56 int Query(int a,int b){
    57     if(a>b)swap(a,b);a++;
    58     return min(Min[a][mm[b-a+1]],Min[b-(1<<mm[b-a+1])+1][mm[b-a+1]]);
    59 }
    60 
    61 int main(){
    62     scanf("%s",s);
    63     len=strlen(s);
    64     s[len]='$';
    65     for(int i=len+1;i<=2*len;i++)
    66         s[i]=s[len*2-i];
    67     len=len*2+1;
    68     for(int i=0;i<len;i++)
    69         r[i]=s[i];
    70     DA(len+1,128);
    71     Lcp(len);
    72     Make_ST(len);
    73     int ans=-1,pos,ret;    
    74     for(int i=0;i<len/2;i++){
    75         ret=Query(rank[i],rank[len-i-1]);//长度为奇数 
    76         if(ret*2-1>ans){
    77             ans=ret*2-1;
    78             pos=i-ret+1; 
    79         }
    80         ret=Query(rank[i],rank[len-i]);//长度为偶数 
    81         if(ret*2>ans){
    82             ans=ret*2;
    83             pos=i-ret;
    84         }
    85     }
    86     
    87     for(int i=0;i<ans;i++)
    88         printf("%c",s[i+pos]);
    89         
    90     printf("
    ");
    91     return 0;    
    92 }
    尽最大的努力,做最好的自己!
  • 相关阅读:
    TCP/IP详解学习笔记(4)-- ARP 和 RARP
    TCP/IP详解学习笔记(3)-- IP:网际协议
    TCP/IP详解学习笔记(2)-- 数据链路层
    TCP/IP详解学习笔记(1)-- 概述
    3D滚动下拉菜单-简直不要太任性
    Bootstrap的字体文件woff2 报错
    一步一步来做WebQQ机器人-(五)(发送消息||完结)
    一步一步来做WebQQ机器人-(四)(获取好友列表和群列表)
    一步一步来做WebQQ机器人-(三)(登录QQ并保持在线)
    一步一步来做WebQQ机器人-(二)(第一次登陆)
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5280657.html
Copyright © 2020-2023  润新知