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 |
---|---|
Kazak | aza |
给定一个字符串,求最长回文子串。
算法分析: 穷举每一位,然后计算以这个字符为中心的最长回文子串。注意这里要分两种情况,一是回文子串的长度为奇数,二是长度为偶数。两种情况都可以转化为求一个后缀和一个反过来写的后缀的最长公共前缀。具体的做法是:将整个字符串反过来写在原字符串后面,中间用一个特殊的字符隔开。这样就把问题变为了求这个新的字符串的某两个后缀的最长公共前缀。 这个做法的时间复杂度为O(nlogn)。如果RMQ问题用时间为O(n)的方1 #include2 #include 3 #include 4 #include 5 #include 6 using namespace std; 7 int const N=2000+3; 8 int wa[N<<1],wb[N<<1],wv[N],rk[N],num[N],sa[N],h[N],L,f[N][12],lg[N<<1]; 9 char s[N<<1]; 10 int cmp(int *r,int x,int y,int z){11 return r[x]==r[y] && r[x+z]==r[y+z]; 12 }13 void build_sa(char *r,int *sa,int n,int m){14 int i,j,p,*x=wa,*y=wb; 15 for(i=0;i =0;i--) sa[--num[x[i]]]=i; 19 for(j=1,p=1;p <<=1,m=p){20 for(p=0,i=n-j;i =j) y[p++]=sa[i]-j; 22 for(i=0;i =0;i--) sa[--num[wv[i]]]=y[i]; 26 swap(x,y); 27 for(i=1,p=1,x[sa[0]]=0;i y) swap(x,y); 53 x++; 54 int k=lg[y-x+1]; 55 return min(f[x][k],f[y-(1< ans || 2*z==ans && k>i-z+1){65 ans=2*z; 66 k=i-z+1; 67 }68 }69 for(int i=0;i ans || 2*z-1==ans && k>i-z+1) {74 ans=2*z-1; 75 k=i-z+1; 76 }77 } 78 for(int i=k;i