Ural 1297 (obtain the longest echo substring, suffix array)

Source: Internet
Author: User
1297. palindrometime limit: 1.0 second
Memory limit: 16 mbthe "U. s. robots "HQ has just initialized ed 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 wowould 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. obviusly, 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 cocould finish that module, so the descrambler will choose the text scanning ction 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. inputthe 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. outputthe longest substring with mentioned property. if there are several such strings you shoshould output the first of them. sample
Input
ThesampletextthatcouldbereadedthesameinbothordersArozaupalanalapuazorA
Output
ArozaupalanalapuazorA

 

 

/*************************************** ******************************** </P> <p> ideas: insert the reverse direction of the original string to the back of the original string, and separate it with 1 in the middle. Finally, separate it with 0. </P> <p> it becomes the longest public prefix of A New String suffix (why? Think for yourself) </P> <p> however, You need to determine whether the answer is in the same position in the original string... I understand this by myself) </P> <p> I wa this question about 20 times before ac--| I am not familiar with the details, the main lack of suffix Array Control </P> <p> This question URL: http://acm.timus.ru/problem.aspx? Space = 1 & num = 1297 </P> <p> ************************** **************************************** * *****/<br/> # include <iostream> <br/> # include <string. h> <br/> using namespace STD; <br/> # define max (A, B) (A> B? A: B) <br/> # define min (A, B) (A> B? B: A) <br/> const int maxn = 100000; </P> <p> int wa [maxn], WB [maxn], WV [maxn], WWS [maxn], a [maxn], sa [maxn], rank1 [maxn], height [maxn]; <br/> int CMP (int * r, int, int B, int L) <br/> {return R [a] = R [B] & R [A + L] = R [B + L];} <br/> void da (int * r, int * Sa, int N, int m) <br/>{< br/> int I, j, P, * x = wa, * Y = WB, * t; <br/> for (I = 0; I <m; I ++) WWS [I] = 0; <br/> for (I = 0; I <n; I ++) WWS [x [I] = R [I] ++; <br/> for (I = 1; I <m; I ++) WWS [I] + = WWS [I-1]; <Br/> for (I = n-1; I> = 0; I --) SA [-- WWS [x [I] = I; <br/> for (j = 1, P = 1; P <n; j * = 2, M = P) <br/> {<br/> for (P = 0, I = N-J; I <n; I ++) y [p ++] = I; <br/> for (I = 0; I <n; I ++) if (SA [I]> = J) Y [p ++] = sa [I]-J; <br/> for (I = 0; I <n; I ++) WV [I] = x [Y [I]; <br/> for (I = 0; I <m; I ++) WWS [I] = 0; <br/> for (I = 0; I <n; I ++) WWS [wv [I] ++; <br/> for (I = 1; I <m; I ++) WWS [I] + = WWS [I-1]; <br/> for (I = n-1; I> = 0; I --) sa [-- WWS [wv [I] = Y [I]; <br/> for (t = x, x = Y, y = T, P = 1, X [SA [0] = 0, I = 1; I <n; I ++) <br/> X [SA [I] = CMP (Y, sa [I-1], sa [I], j )? P-1: P ++; <br/>}< br/> return; <br/>}</P> <p> void calheight (int * r, int * Sa, int N) <br/>{< br/> int I, j, k = 0; <br/> for (I = 1; I <= N; I ++) rank1 [SA [I] = I; <br/> for (I = 0; I <n; height [rank1 [I ++] = K) <br/> for (K? K --: 0, j = sa [rank1 [I]-1]; R [I + k] = R [J + k]; k ++ ); <br/>}</P> <p> int N, Len; </P> <p> bool ask (int A, int B) <br/>{< br/> if (a <Len & B> Len) return true; <br/> else return false; <br/>}</P> <p> int main () <br/> {<br/> char STR [1005], str1 [1005]; <br/> int I, j, Max, start; <br/> while (CIN> Str) <br/>{< br/> n = 0; <br/> Len = strlen (STR); <br/> for (I = 0; I <Len; I ++) <br/> str1 [I] = STR [len-1-i]; <br/> for (I = 0; I <Len; I ++) <br/>{< br/> A [n ++] = static_cast <int> (STR [I]); <br/>}< br/> A [n + +] = 1; <br/> for (I = 0; I <Len; I ++) <br/>{< br/> A [n ++] = static_cast <int> (str1 [I]); <br/>}< br/> A [n] = 0; <br/> da (A, SA, N + 1,250); <br/> calheight (, sa, n); <br/> max = 1; // remember to start from 1 (I started from 0 and Wa many times @) <br/> Start = 0; <br/> int low, high; <br/> for (I = 1; I <= N; I ++) <br/> {<br/> low = min (SA [I-1], sa [I]); <br/> high = max (SA [I-1], sa [I]); <br/> If (ASK (low, high) & (low + height [I]) = (n-high ))) <br/>{< br/> If (max <peight [I]) <br/>{< br/> max = height [I]; <br/> Start = low; <br/>}< br/> else if (max = height [I]) <br/>{< br/> Start = min (START, low ); <br/>}< br/> for (I = start, j = 0; j <Max; I ++, j ++) <br/>{< br/> cout <STR [I]; <br/>}< br/> cout <Endl; <br/>}< br/> return 0; <br/>}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.