多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內(nèi)最全IT社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當前位置:首頁 > 互聯(lián)網(wǎng) > URAL - 1297 Palindrome(后綴數(shù)組求最長回文子串)

URAL - 1297 Palindrome(后綴數(shù)組求最長回文子串)

來源:程序員人生   發(fā)布時間:2014-09-30 02:57:27 閱讀次數(shù):3170次

Description

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

input output
ThesampletextthatcouldbereadedthesameinbothordersArozaupalanalapuazorA
ArozaupalanalapuazorA


題意:求最長回文子串。
思路:窮舉每一位,然后計算以這個字符為中心的最長回文子串。注意這里要分兩種情況,一是回文子串的長度為奇數(shù),二是長度為偶數(shù)。兩種情況都可以轉(zhuǎn)化為求一個后綴和一個反過來寫的后綴的最長公共前綴。具體的做法是:將整個字符串反過來寫在原字符串后面,中間用一個特殊的字符隔開。這樣就把問題變?yōu)榍筮@個新的字符串的某兩個后綴的最長公共前綴。然后在查找最長公共前綴lcp的時候利用到了RMQ,我們知道對于兩個后綴j和k,設(shè)rank[j]<rank[k],則不難證明后綴j和k的LCP長度等于height[rank[j]+1],height[rank[j]+2],...,height[rank[k]]中的最小值,

即RMQ(height, rank[j]+1, rank[k]),這是通過height[]數(shù)組的兩兩比較做到的

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; const int maxn = 2010; int sa[maxn]; //SA數(shù)組,表示將S的n個后綴從小到大排序后把排好序的 //的后綴的開頭位置順次放入SA中 int t1[maxn], t2[maxn], c[maxn]; int rank[maxn], height[maxn]; int s[maxn], r[maxn]; char str[maxn]; int st[maxn][20]; void build_sa(int s[], int n, int m) { int i, j, p, *x = t1, *y = t2; for (i = 0; i < m; i++) c[i] = 0; for (i = 0; i < n; i++) c[x[i] = s[i]]++; for (i = 1; i < m; i++) c[i] += c[i-1]; for (i = n-1; i >= 0; i--) sa[--c[x[i]]] = i; for (j = 1; j <= n; j <<= 1) { p = 0; for (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 < m; i++) c[i] = 0; for (i = 0; i < n; i++) c[x[y[i]]]++; for (i = 1; i < m; i++) c[i] += c[i-1]; for (i = n-1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i]; swap(x, y); p = 1, x[sa[0]] = 0; for (i = 1; i < n; i++) x[sa[i]] = y[sa[i-1]] == y[sa[i]] && y[sa[i-1]+j] == y[sa[i]+j] ? p-1 : p++; if (p >= n) break; m = p; } } void getHeight(int s[],int n) { int i, j, k = 0; for (i = 0; i <= n; i++) rank[sa[i]] = i; for (i = 0; i < n; i++) { if (k) k--; j = sa[rank[i]-1]; while (s[i+k] == s[j+k]) k++; height[rank[i]] = k; } } void ST(int n) { for (int i = 1; i <= n; i++) st[i][0] = i; for (int j = 1; (1<<j) <= n; j++) { for (int i = 1; i + (1<<j) <= n; i++) { int p = st[i][j-1]; int q = st[i+(1<<(j-1))][j-1]; st[i][j] = height[p] > height[q] ? q : p; } } } int RMQ(int i, int j) { int k = 0; if (i > j) swap(i, j); i++; while ((1<<(k+1)) <= j-i+1) k++; i = st[i][k]; j = st[j-(1<<k)+1][k]; return min(height[i], height[j]); } int main() { while (scanf("%s", str) != EOF) { int len = strlen(str); int n = 2 * len + 1; for (int i = 0; i < len; i++) r[i] = str[i]; r[len] = 1; for (int i = 0; i < len; i++) r[i+len+1] = str[len-1-i]; r[n] = 0; //notice build_sa(r, n+1, 128); getHeight(r, n); ST(n); int mid = n >> 1; int ans = 0, cur = 0; for (int i = 0; i < mid; i++) { int j = RMQ(rank[i], rank[n-i-1]); //奇對稱 if ((j<<1) - 1 > ans) { ans = (j<<1) - 1; cur = i - j + 1; } if (i) { j = RMQ(rank[i], rank[n-i]); //偶對稱 if ((j << 1) > ans) { ans = j << 1; cur = i - j; } } } for (int i = cur; i < cur + ans; i++) printf("%c", r[i]); printf(" "); } return 0; }

生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 中文字幕 亚洲 一区二区三区 | 国产精品久久在线观看 | www.日本免费 | 最近中文字幕无吗免费高清 | 国产精品一区二区久久精品涩爱 | 亚洲第一成人在线 | 欧美日韩一区二区三区久久 | 爱爱a | 国产主播福利在线 | 亚洲黄色一区二区 | 性欧美性| 欧美大穴 | 国产成人黄网址在线视频 | 最近最新中文字幕免费高清1 | www.日本xxxx | 久久久久久久国产精品视频 | 老司机精品99在线播放 | 久久riav | 在线看片亚洲 | 国产精品一级视频 | 亚洲乱码在线观看 | 亚洲国产精品久久综合 | 国产农村精品一级毛片视频 | 国产欧美精品综合一区 | 精品国产福利片在线观看 | 手机在线看片福利盒子 | 亚洲图片欧美在线 | 春色在线 | 性欧美videofree另类hd | 亚洲第一成年人网站 | 久久综合九色综合97欧美 | 亚洲毛片网| 欧美日韩亚洲天堂 | 欧美午夜免费一级毛片 | 久久国产欧美另类久久久 | 国产精品一区高清在线观看 | 欧美xxxx做受视频 | 91久久人澡人人添人人爽 | 精品国产亚洲一区二区三区 | 4四虎44虎www在线影院麻豆 | 亚洲精品美女久久777777 |