Ural1297Palindrome最长回文串.manacher解法
2016-10-07 16:47
351 查看
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
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 |
#include<iostream> #include<cstring> #include<cstdio> using namespace std; char a[3000], b[3000]; int Len[3000]; void init(int len) { b[0] = '@'; for(int i = 1; i <= len*2; i += 2) { b[i] = '#'; b[i+1] = a[i/2]; } b[len*2+1] = '#'; b[len*2+2] = '$'; b[len*2+3] = '\0'; } int manacher(int len, int &p) { int mx = 0, po = 0, ans = 0; for(int i = 1; i <= 2*len +1; i++) { if(i < mx) Len[i] = min(mx - i, Len[2*po-i]); else Len[i] = 1; while(b[i - Len[i]] == b[i+Len[i]]) Len[i]++; if(Len[i] + i > mx) { mx = Len[i] + i; po = i; } if(Len[i] - 1 > ans) { ans = Len[i] - 1; p = i; } } return ans; } int main() { int len, p, ans, be, en; while(scanf("%s", a) != EOF) { memset(Len, 0, sizeof(Len)); memset(b, 0, sizeof(b)); len = strlen(a); init(len); ans = manacher(len, p); be = p - ans + 1; en = p + ans - 1; for(int i = be; i <= en; i++) { if(b[i] != '#') cout << b[i]; } cout << endl; } return 0; }
相关文章推荐
- bzoj 2565: 最长双回文串 manacher算法
- HDU3068 URAL1297 Manacher求最长回文子串
- Manacher算法 O(n)求最长回文串
- hdu 3068 && pku 3974 (最长回文串)(Manacher 算法)
- HYSBZ - 2565 最长双回文串 Manacher
- bzoj 2565: 最长双回文串 manacher
- 最长回文串的解法---Manacher算法
- manacher算法求最长回文串 和 hdu 3068 最长回文串
- Manacher 计算最长回文串
- bzoj 2565 最长双回文串 manacher
- BZOJ 2565 最长双回文串 Manacher
- 最长回文串O(N)算法 manacher
- BZOJ 2565 最长双回文串(manacher)
- bzoj 2565 最长双回文串 - Manacher
- HDU3068 最长回文串 解题报告【字符串】【Manacher】
- Manacher’s Algorithm( O(n)最长回文串)
- URAL 1297(Palindrome,后缀数组)最长回文串
- BZOJ 2565: 最长双回文串 [Manacher]
- bzoj 2565: 最长双回文串 manacher
- 字符串的最长回文串:Manacher’s Algorithm