HDU 5340 three palindromes (string processing + palindrome)

Source: Internet
Author: User

HDU 5340 Three Palindromes

Problem Description
Determines whether the string s can be divided into three non-empty palindrome strings.
Enter a description
The first line is an integer t that represents the number of data groups. T \leq 20t≤20
For each group, only a string consisting of lowercase letters is included. 1 \leq | s| \leq 200001≤∣s∣≤20000
Output Description
For each group, the single-line output is "Yes" or "No".
Input Sample
2
Abc
Abaadada
Output Sample
Yes
No

Topic: Give a string, determine whether it can be divided into three non-empty palindrome string. Problem-solving idea: First use manacher algorithm to find the longest palindrome range that can be expanded at the center of each point. You can then try out two arrays of pre[i] and Pos[i], recording the cut at I point, can make the substring before or after I, become a palindrome string. After the pre and POS arrays are found, traverse the center of the middle section of the string, each finding a center point that expands to its center (1 < A < P[i]-1) when pre[i-a] and Pos[i + A] are marked, then the cut is valid.
#include <cstdio>#include <cstring>#include <algorithm>using namespace STD;Const intN =20005;Charstr[n<<1], s[n];intp[n<<1], Len, len2;voidManacher () {len =strlen(s); for(inti =0; i < Len; i++) {str[i<<1] =' # '; str[(i<<1) +1] = S[i]; } str[len<<1] =' # '; str[(len<<1) +1] =' + '; Len =strlen(str);intMAXP =0, id =0; for(inti =0; i < Len; i++) {if(Maxp > i) p[i] = min (maxp-i, p[(id<<1)-i]);ElseP[i] =1; for(; str[i-p[i] [= Str[i + p[i]] && i + p[i] < len && I-p[i] >=0; p[i]++);if(i + p[i] > Maxp)            {MAXP = p[i] + i;        id = i; }    }}BOOLa[40005], b[40005];BOOLMark () {len =strlen(s); Len2 = (len<<1)  +1;memsetA0, len2);memsetB0, len2);if(len = =3)return true;if(Len <3)return false; for(inti =1; i < len2; i++) {if(P[i]-1= = i) A[i + (P[i]-1)] =true;if(P[len2-1-I]-1= = i) B[len2-1-I-(P[LEN2-1-I]-1)] =true; } for(inti =1; I < len2-1; i++) { for(intj =1; J < P[i]; J + +) {if(A[i-j] & B[i + j]) {return true; }           }    }return false;}intMain () {intTscanf("%d", &t); while(t--) {scanf('%s ', s); Manacher ();if(Mark ()) {printf("yes\n"); }Else printf("no\n"); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission can also be reproduced, but to indicate the source oh.

HDU 5340 three palindromes (string processing + palindrome)

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.