URAL1297: Palindrome (suffix array), ural1297palindrome

Source: Internet
Author: User

URAL1297: Palindrome (suffix array), ural1297palindrome

Description

The "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.

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 shoshould output the first of them.

Sample Input

Input Output
ThesampletextthatcouldbereadedthesameinbothordersArozaupalanalapuazorA
ArozaupalanalapuazorA

Source

Problem Author:Eugene krohalev
Problem Source:IX Open Collegiate Programming Contest of the High School Pupils (13.03.2004)

Or the topic in the paper first reverses, Concatenates the original string and the reverse string, and then finds the common prefix for the two. The only note is that if multiple groups need to output the first group
# Include <iostream> # include <stdio. h> # include <string. h> # include <stack> # include <queue> # include <map> # include <set> # include <vector> # include <math. h> # include <bitset> # include <algorithm> # include <climits> using namespace std; # define LS 2 * I # define RS 2 * I + 1 # define UP (I, x, y) for (I = x; I <= y; I ++) # define DOWN (I, x, y) for (I = x; I> = y; I --) # define MEM (a, x) memset (a, x, sizeof (a) # define W (a) while (a) # define gcd (a, B) _ Gcd (a, B) # define LL long # define N 2222 # define MOD 1000000007 # define INF 0x3f3f3f3f # define EXP 1e-8int wa [N], wb [N], wsf [N], wv [N], sa [N]; int rank [N], height [N], s [N], a [N]; char str [N], str1 [N], str2 [N]; // sa: the starting position of the I-bits in the Lexicographic Order in str sa [I] // rank: is the str I position suffix is in the Lexicographic Order of the nth // height: Lexicographic Order of I and I-1 Suffix of the longest common prefix int cmp (int * r, int, int B, int k) {return r [a] = r [B] & r [a + k] = r [B + k];} void getsa (int * r, int * sa, int n, int m) // n must contain 0 {int I, j, p, * x = wa, * y = wb, * t; for (I = 0; I <m; I ++) added at the end) wsf [I] = 0; for (I = 0; I <n; I ++) wsf [x [I] = r [I] ++; for (I = 1; I <m; I ++) wsf [I] + = wsf [I-1]; for (I = n-1; I> = 0; I --) sa [-- wsf [x [I] = I; p = 1; j = 1; for (; p <n; j * = 2, m = p) {for (p = 0, I = n-j; I <n; I ++) y [p ++] = I; for (I = 0; I <n; I ++) if (sa [I]> = j) y [p ++] = sa [I]-j; for (I = 0; I <n; I ++) wv [I] = x [y [I]; for (I = 0; I <m; I ++) wsf [I] = 0; for (I = 0; I <n; I ++) wsf [wv [I] ++; for (I = 1; I <M; I ++) wsf [I] + = wsf [I-1]; for (I = n-1; I> = 0; I --) sa [-- wsf [wv [I] = y [I]; t = x; x = y; y = t; x [sa [0] = 0; for (p = 1, I = 1; I <n; I ++) x [sa [I] = cmp (y, sa [I-1], sa [I], j )? P-1: p ++ ;}} void getheight (int * r, int n) // n does not save the last 0 {int I, j, k = 0; for (I = 1; I <= n; I ++) rank [sa [I] = I; for (I = 0; I <n; I ++) {if (k) k --; else k = 0; j = sa [rank [I]-1]; while (r [I + k] = r [j + k]) k ++; height [rank [I] = k ;}} int main () {int len, n = 0, I, j, k; W (~ Scanf ("% s", str) {n = 0; len = strlen (str); UP (I, 0, len-1) s [n + +] = str [I]; s [n + +] = 200; DOWN (I, len-1, 0) s [n ++] = str [I]; s [n] = 0; getsa (s, sa, n + 1,300); getheight (s, n ); int ans = 1, x = 0; UP (I, 1, n-1) {int minn = min (sa [I], sa [I-1]); int maxn = max (sa [I], sa [I-1]); if (minn> = len | maxn <len) continue; // if (minn + height [I]! = N-maxn) continue; // minn + height [I] is the last of the two public prefixes, the corresponding position is n-(n-sa [k]) in the next string, that is, if (height [I]> ans) in n-maxn) {ans = height [I]; x = minn;} else if (height [I] = ans) {x = min (minn, x );}} for (I = x; ans --; I ++) printf ("% c", str [I]); puts ("");}}


Related Article

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.