题目链接:https://vjudge.net/problem/URAL-1297
1297. Palindrome
Time limit: 1.0 second
Memory limit: 64 MB
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.
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 |
Problem Author: Eugene Krokhalev
Problem Source: IX Open Collegiate Programming Contest of the High School Pupils (13.03.2004)
Problem Source: IX Open Collegiate Programming Contest of the High School Pupils (13.03.2004)
Tags: palindromes string algorithms
题解:
代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #include <cmath> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 using namespace std; 13 typedef long long LL; 14 const int INF = 2e9; 15 const LL LNF = 9e18; 16 const int MOD = 1e9+7; 17 const int MAXN = 1e4+100; 18 19 bool cmp(int *r, int a, int b, int l) 20 { 21 return r[a]==r[b] && r[a+l]==r[b+l]; 22 } 23 24 int r[MAXN], sa[MAXN], Rank[MAXN], height[MAXN]; 25 int t1[MAXN], t2[MAXN], c[MAXN]; 26 void DA(int str[], int sa[], int Rank[], int height[], int n, int m) 27 { 28 n++; 29 int i, j, p, *x = t1, *y = t2; 30 for(i = 0; i<m; i++) c[i] = 0; 31 for(i = 0; i<n; i++) c[x[i] = str[i]]++; 32 for(i = 1; i<m; i++) c[i] += c[i-1]; 33 for(i = n-1; i>=0; i--) sa[--c[x[i]]] = i; 34 for(j = 1; j<=n; j <<= 1) 35 { 36 p = 0; 37 for(i = n-j; i<n; i++) y[p++] = i; 38 for(i = 0; i<n; i++) if(sa[i]>=j) y[p++] = sa[i]-j; 39 40 for(i = 0; i<m; i++) c[i] = 0; 41 for(i = 0; i<n; i++) c[x[y[i]]]++; 42 for(i = 1; i<m; i++) c[i] += c[i-1]; 43 for(i = n-1; i>=0; i--) sa[--c[x[y[i]]]] = y[i]; 44 45 swap(x, y); 46 p = 1; x[sa[0]] = 0; 47 for(i = 1; i<n; i++) 48 x[sa[i]] = cmp(y, sa[i-1], sa[i], j)?p-1:p++; 49 50 if(p>=n) break; 51 m = p; 52 } 53 54 int k = 0; 55 n--; 56 for(i = 0; i<=n; i++) Rank[sa[i]] = i; 57 for(i = 0; i<n; i++) 58 { 59 if(k) k--; 60 j = sa[Rank[i]-1]; 61 while(str[i+k]==str[j+k]) k++; 62 height[Rank[i]] = k; 63 } 64 } 65 66 int dp[MAXN][20], mm[MAXN]; 67 void initRMQ(int n, int b[]) 68 { 69 mm[0] = -1; 70 for(int i = 1; i<=n; i++) 71 dp[i][0] = b[i], mm[i] = ((i&(i-1))==0)?mm[i-1]+1:mm[i-1]; 72 for(int j = 1; j<=mm[n]; j++) 73 for(int i = 1; i+(1<<j)-1<=n; i++) 74 dp[i][j] = min(dp[i][j-1], dp[i+(1<<(j-1))][j-1]); 75 } 76 77 int RMQ(int x, int y) 78 { 79 if(x>y) swap(x, y); 80 x++; 81 int k = mm[y-x+1]; 82 return min(dp[x][k], dp[y-(1<<k)+1][k]); 83 } 84 85 char str[MAXN]; 86 int main() 87 { 88 scanf("%s" ,str); 89 int len = strlen(str); 90 int n = 2*len+1; 91 for(int i = 0; i<len; i++) 92 r[i] = r[n-1-i] = str[i]; 93 r[len] = '$'; r[n] = 0; 94 DA(r, sa, Rank, height, n, 128); 95 initRMQ(n, height); 96 97 int st, ans = 0, lcp; 98 for(int i = 0; i<len; i++) 99 { 100 lcp = RMQ(Rank[i], Rank[n-1-i]); //奇数,以i为中点匹配 101 if(2*lcp-1>ans) 102 ans = 2*lcp-1, st = i-lcp+1; 103 104 if(i==0) continue; 105 lcp = RMQ(Rank[i], Rank[n-i]); //偶数, 逆串往后退一个位置进行匹配 106 if(2*lcp>ans) 107 ans = 2*lcp, st = i-lcp; 108 } 109 110 for(int i = st; i<st+ans; i++) 111 putchar(str[i]); 112 putchar(' '); 113 }