HDU 3294 Girls ' (Manacher algorithm + recording interval)

Source: Internet
Author: User


Girls ' Time limit:3000/1000 MS (java/others) Memory limit:65535/32768 K (java/others) Total Submission (s): 566 Accepted Submission (s): 212

problem DescriptionOne day, Sailormoon Girls is so delighted the they intend to the, about palindromic strings. Operation contains, steps:
First Step:girls would write a long string (only contains lower case) on the paper. For example, "ABCDE", but ' a ' inside was not the real ' a ', so means if we define the ' B ' is the real ' a ' and then we can inf Er that ' C ' are the real ' B ', ' d ' is the real ' C ' ..., ' a ' is the real ' z '. According to this, the string "ABCDE" Changes to "Bcdef".
Second Step:girls'll find out the longest palindromic string in the given string, the length of palindromic string must be equal or more than 2. 
InputInput contains multiple cases.
Each case contains parts, a character and a string, they is separated by one space, the character representing the RE Al ' A ' is and the length of the string won't exceed 200000.All input must be lowercase.
If the length of string is Len, it's marked from 0 to len-1. 
OutputPlease execute the operation following the steps.
If you find one, output the start position and end position of palindromic string in a line, next line output the real pal Indromic string, or output "No solution!".
If There is several answers available, please choose the string which first appears. 
Sample Input
b babda ABCD
 
Sample Output
0 2azaNo solution!
 Authorwangjing1111 Source2010 "Hdu-sailormoon" Programming Contest

Topic Link: http://acm.hdu.edu.cn/ showproblem.php?pid=3294

Topic: Enter a character ch and a string, ask if the CH as ' a ', each character of the string should also be changed, such as B AA, if B is ' a ', then B is the ' a ' in front of ' Z ' ', here is the loop representation, the longest palindrome substring of the output string, if the longest palindrome string length is 1, the output no solution!

Topic Analysis: String according to the requirements of change, run once manacher, each update Maxl, respectively, record the position of the left and right end in the original string

#include <cstdio> #include <cstring> #include <algorithm>using namespace std;int const MAX = 200005; Char S[max << 1], Save[max << 1];int p[max << 1], L, R;int Manacher () {int len = strlen (s), Maxp = 0, MAX L = 0;for (int i = len; I >= 0; i--) {S[i * 2 + 2] = s[i];s[i * 2 + 1] = ' # ';} S[0] = ' * '; for (int i = 2; i < 2 * len + 1; i++) {if (P[maxp] + maxp > i) p[i] = min (p[2 * maxp-i], P[MAXP] + maxp-i  ), else p[i] = 1;while (s[i-p[i] [= S[i + p[i]]) p[i]++;if (P[MAXP] + Maxp < i + p[i]) Maxp = i;if (Maxl < p[i]) {L = (i -p[i])/2;r = (i + p[i])/2-2;maxl = P[i];}} return MAXL-1;} int main () {int Ans;char ch[2];while (scanf ("%s%s", CH, s)! = EOF) {int len = strlen (s); L = r = 0;for (int i = 0; i < Len; i++) {int tmp = S[i]-ch[0];if (TMP < 0) TMP = + tmp;s[i] = ' a ' + tmp;} strcpy (save, s), int ans = manacher (), if (ans = = 1) printf ("No solution!\n"), else{printf ("%d%d\n", L, R); for (int i = l; I &l T;= R; i++) printf ("%c", Save[i]);p rintf ("\ n");}}



HDU 3294 Girls ' (Manacher algorithm + recording interval)

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.