LA 4975 (Casting spells-manacher+ violence search)

Source: Internet
Author: User

4975-casting spellstime limit:3.000 seconds

Casting spells is the least understood technique of dealing with real life. Actually, people find it quite hard-distinguish between a real spells like ' Abrahellehhelleh ' (used in the battles and Taught at the Mage universities) and screams as "Rachelhellabracadabra" (used by uneducated witches for shouting at CA TS).

Finally, the conducted at the unheard University showed how one can measure the power of a word (be it a real spe ll or a scream). It appeared that it was connected with the mages ' ability to pronounce words backwards. (Actually, some singers were burned at the stake for exactly the same ability, as it is perceived as demonic possession.) Namely, the power of a word is the length of the maximum subword of the formwwrwwr (where W is an arbitrary sequence of characters and WR isw written backwards). If No such subword exists, then the power of the word is 0. For example, the power ofAbrahellehhelleh is as it contains Hellehhelleh and the power of Rachelhellabrac Adabra is 0. Note that the power of a word are always a multiple of 4.

Input

The input contains several test cases. The first line of the input contains a positive integerZ, denoting the number of the test cases. ThenZ -Test cases follow, each conforming to the format described below.

The input is one line containing a word's length at most 3.consisting of (large or small) letters of the E Nglish alphabet.

OutputFor each test case, the your program have to write a output conforming to the format described below.

You should output one integer K being the power of the word.

Sample Input

2 Abrahellehhelleh Rachelhellabracadabra

Sample Output

12 0


This problem first use Manacher to get the radius of each character, and then find this string of half or palindrome string palindrome string

LRJ said the problem to use a special data structure, my RMQ time-out, violence over ...



#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include < functional> #include <iostream> #include <cmath> #include <cctype> #include <ctime>using namespace std; #define for (I,n) for (int. i=1;i<=n;i++) #define FORK (I,k,n) for (int. i=k;i<=n;i++) #define REP (I,n) for (int i=0;i<n;i++) #define ForD (I,n) for (int. i=n;i;i--) #define REPD (I,n) for (int. i=n;i>=0;i--) #define FORP (x) for ( int p=pre[x];p; p=next[p]) #define FORPITER (x) for (int &p=iter[x];p; p=next[p]) #define LSON (x<<1) #define Rson ((x<<1) +1) #define MEM (a) memset (A,0,sizeof (a)), #define MEMI (a) memset (A,127,sizeof (a)), #define MEMI (a) memset ( A,128,sizeof (a)), #define INF (2139062143) #define F (100000007) #define MAXN (300000+10) #define MAXSTR (maxn*2+2) #define Maxlog (+) #define SP_CHAR1 (' * ') #define SP_CHAR2 ('_') typedef long Long Ll;ll Mul (ll A,ll b) {return (a*b)%F;} ll Add (ll A,ll b) {return (a+b)%F;} ll Sub (ll A,ll b) {return (a-b+ (a)/f*f+f)%F; void UPD (ll &a,ll b) {a= (a%f+b%f)%F;} Class Manacher{public:int N;char s[maxn];int p[maxstr];manacher () {n=0; MEM (s) mem (P)}manacher (char *_s) {n=0; MEM (s) if (_s) memcpy (s,_s,sizeof (char) * (strlen (_s) +1)), N=strlen (s); MEM (P)}void mem (char *_s) {n=0; MEM (s) if (_s) memcpy (s,_s,sizeof (char) * (strlen (_s) +1)), N=strlen (s); MEM (p)}char str[maxstr];void work () {str[0]=sp_char1; Rep (I,n) str[2*i+1]=sp_char2,str[2*i+2]=s[i]; STR[2*N+1]=SP_CHAR2; str[2*n+2]= ' + '; n=2*n+2; MEM (p) int mx=0,id=0; for (i,n-1) {if (I&LT;MX) p[i]=min (P[2*id-i],mx-i), while (Str[i-p[i]]==str[i+p[i]]) ++p[i];if (Mx<i+p[i])// MX is the most right end of the identified {Mx=i+p[i];id=i;}}} S;int T;char S[maxn];int Main () {freopen ("la4975.in", "R", stdin);//freopen (". Out", "w", stdout); Cin>>t;while ( t--) {scanf ("%s", s); S.mem (s); S.work ();//printf ("%s", S.STR); Rep (I,S.N) cout<<s.str[i]<< ': ' <<i<< '; int ans=0;for (int i=1;i<=s.n-1;i+=2) {if (S.p[i] ==1) continue;for (int l = (s.p[i]-1)/4*4; l>ans; l-=4) {if (s.p[i-l/2]>=l/2&&s.p[I+L/2]&GT;=L/2) Ans=max (ans,l);}} cout<<ans<<endl; }return 0;}





LA 4975 (Casting spells-manacher+ violence search)

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.