To give you a string that allows you to find the longest continuous palindrome that exists.
Problem-solving idea: The string is added to the array first, and then solved with the suffix array. Two methods: 1, enumeration rank, directly compare rank the same string where the difference is not Len. If yes, the record is solved; 2, enumerate the addresses, and find the maximum value of the 2*len-i+1 and the LCP of the first address.
PS: It is important to note that if you output a string in front of multiple solutions.
The two methods are written together, Del, and Del1 function respectively.
1297. Palindrometime 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? Have infiltrated into "U.S. robotics". ? U.S. Robots? Security Service would has already started an undercover operation to establish the agent's identity, but, fortunately, t He letter describes communication channel the agent uses. He'll publish articles containing stolen data to the "Solaris" Almanac. Obviously, he'll obfuscate the data, so "Robots Unlimited" would have to use a special Descrambler ("Robots Unlimited" pa RT number NPRx8086, specifications is kept secret). Have read the letter, the "U.S. Robots" President recalled have 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 have finished work on the NPRx8086 design. So, the president have 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 h Aystack. 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 is working on a specific module, the text direction detector. Nobody else could finish that module, so the Descrambler would choose the text scanning direction at random. To ensure the correct descrambling of the "the message by NPRx8086, agent must encode" information in such a "that the RE Sulting secret message reads the same both forwards and backwards.
In addition, it's reasonable to assume that the agent would be being sending a very long message, so John have 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 would remove them from the text before feeding it into the pro Gram. Inputthe input consists of a single line, which contains a string of Latin alphabet letters (no other characters would appe AR in the string). String length would not exceed characters. Outputthe longest substring with the mentioned property. If There is several such strings you should output the first of them. Sample
| input |
Output |
Thesampletextthatcouldbereadedthesameinbothordersarozaupalanalapuazora |
Arozaupalanalapuazora |
problem Author:Eugene Krokhalev
problem Source:IX Open Collegiate Programming Contest of the High School pupils (13.03.2004)
Tags:()
#include <algorithm> #include <iostream> #include <stdlib.h> #include <string.h> #include < iomanip> #include <stdio.h> #include <string> #include <queue> #include <cmath> #include < stack> #include <ctime> #include <map> #include <set> #define EPS 1e-9///#define M 1000100///#define ll __int64#define ll Long long///#define INF 0x7ffffff#define inf 0x3f3f3f3f#define PI 3.1415926535898#define Zero (x) ((FA BS (x) <eps)? 0:x) #define MOD 1000000007#define Read () freopen ("Autocomplete.in", "R", stdin) #define Write () freopen (" Autocomplete.out "," w ", stdout) #define CIN () Ios::sync_with_stdio (false) using namespace Std;inline int Read () {char ch; BOOL flag = FALSE; int a = 0; while (!) ( ((ch = getchar ()) >= ' 0 ') && (ch <= ' 9 ')) | | (ch = = '-'))); if (ch! = '-') {a *= 10; A + = ch-' 0 '; } else {flag = true; } while (((ch = getchar ()) >= ' 0 ') && (ch <= ' 9 ')) { A *= 10; A + = ch-' 0 '; } if (flag) {a = A; } return A; void write (int a) {if (a < 0) {Putchar ('-'); A =-A; } if (a >=) {write (A/10); } putchar (a% 10 + ' 0 ');} const int MAXN = 20050;int WA[MAXN], WB[MAXN], WV[MAXN], ws1[maxn];int sa[maxn];int cmp (int *r, int A, int b, int l) {R Eturn R[a] = = R[b] && r[a+l] = = R[b+l];} void da (int *r, int *sa, int n, int m) {int I, J, p, *x = WA, *y = WB; for (i = 0; i < m; i++) ws1[i] = 0; for (i = 0; i < n; i++) ws1[x[i] = r[i]]++; for (i = 1; i < m; i++) ws1[i] + = ws1[i-1]; for (i = n-1; I >= 0; i--) sa[--ws1[x[i]] = i; for (j = 1, p = 1; p < n; j <<= 1, 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++) ws1[i] = 0; for (i = 0; i < n; i++) ws1[wv[i]]++; for (i = 1; i < m; i++) ws1[i] + = ws1[i-1]; for (i = n-1; I >= 0; i--) sa[--ws1[wv[i]] = y[i]; for (Swap (x, y), p = 1, x[sa[0]] = 0, i = 1; i < n; i++) x[sa[i]] = cmp (y, sa[i-1], Sa[i], j)? p-1:p++; }}int RANK[MAXN], height[maxn];void calheight (int *r, int *sa, int n) {int I, j, k = 0; for (i = 1; I <= n; i++) rank[sa[i]] = i; for (int i = 0; i < n; height[rank[i++]] = k) for (k?k--:0, j = sa[rank[i]-1]; r[i+k] = = R[j+k]; k++); return;} int dp[maxn][30];void RMQ (int len) {for (int i = 1; I <= len; i++) dp[i][0] = Height[i]; for (int j = 1; 1<<j <= Maxn; j + +) {for (int i = 1; i+ (1<<j)-1 <= len; i++) Dp[i][j] = Min (dp[i][j-1], dp[i+ (1<< (j-1))][j-1]); }}int lg[maxn];int querry (int l, int r) {int k = lg[r-l+1]; return min (Dp[l][k], dp[r-(1<<k) +1][k]);} void Init () {lg[0] =-1; for (int i = 1; i < MAXN; ++i) lg[i] = Lg[i>>1] + 1;} Char str[maxn];int seq[maxn];void Del1 (int n) {int Max = 0; int pos = 0; for (int i = 0; i < n; i++) {int l = rank[i]; int r = rank[2*n-i+1]; if (L > R) Swap (L, R); int tmp = Querry (l+1, R); if (Tmp*2 > Max) {max = tmp*2; pos = i; } L = Rank[i]; r = Rank[2*n-i]; if (L > R) Swap (L, R); TMP = Querry (l+1, R); if (tmp*2-1 > Max) {max = tmp*2-1; pos = i; }} for (int i = POS-MAX/2; max--; ++i) Putchar (Seq[i]); Puts (""); return;} void Del (int n, int len) {int Max = 1; int pos = 0; for (int i = 1; I <= n; i++) {int tmp = Height[i]; int l = sa[i-1]; int r = Sa[i]; if (L > R) Swap (L, R); if (l >= len | | r <= len) continue; if (l+tmp! = n-r) Continue; if (tmp > Max) {max = tmp; pos = l; } else if (tmp = = Max && L < pos) pos = l; } for (int i = pos, ans = 0; ans < Max; i++, ans++) printf ("%c", Seq[i]); Puts (""); return;} int main () {///init (); while (~SCANF ("%s", str)) {int len = strlen (str); int ans = 0; for (int i = 0; i < len; i++) seq[ans++] = Str[i]; seq[ans++] = 1; for (int i = len-1; I >= 0; i--) seq[ans++] = Str[i]; Seq[ans] = 0; int Len = ans; Da (seq, SA, len+1, 128); Calheight (seq, SA, Len); Del (ans, Len);////enumeration position to determine if the LCP is a position difference; Del1 (len);////enumeration position to determine the LCP of the interval; } return 0;}
URAL 1297. palindrome (suffix array to find the maximum palindrome string)