palindromic substringtime limit:10000msmemory limit:65536kbthis problem would be judged onHDU. Original id:4426

64-bit integer IO format: %i64d Java class name: Main In the Kingdom of string, people like palindromic strings very much. They like only palindromic strings and dislike all other strings. There is a unified formula to calculate the score of a palindromic string. The score is calculated by applying the following three steps.

**1.**Since a palindromic string is symmetric, the second half (excluding the middle of the string if the length are odd) is got Rid of, and only the rest is considered. For example, "ABBA" becomes "AB", "ABA" becomes "AB" and "Abacaba" becomes "Abac".

**2.**Define some integer values for ' a ' to ' Z '.

**3.**Treat the rest part as a 26-based number M and the score are M modulo 777,777,777.

However, different person may has different values for ' a ' to ' Z '. For example, if ' a ' was defined as 3, ' B ' is defined as 1 and C was defined as 4, then the string "Accbcca" had the score (3 x263+4x262+4x26+1) modulo 777777777=55537.

One day, a very long string S are discovered and everyone in the kingdom wants to know that among all the palindromic subst Rings of S, what the one and the k-th smallest score is.

InputThe first line contains an integer T (1≤t≤20), the number of test cases.

The first line in each case contains-integers n, m (1≤n≤100000, 1≤m≤20) where n is the length of S and M are th E number of people in the kingdom. The second line was the string S consisting of only lowercase letters. The next m lines each containing integers describes a person in the following format.

Ki va vb ... vz

Where VA is the value of ' a ' for the person, VB is the value of ' B ' and so on. It is ensured that the ki-th smallest palindromic substring exists and VA, VB, ..., VZ is in the range of [0, 26]. But the values may coincide.

OutputFor each person, output the score of the k-th smallest palindromic substring on one line. Print a blank line after each case.Sample Input

2ABCDCA3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 17 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 10ZZZZ1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 142 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 143 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 144 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 146 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 148 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 149 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1410 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1451 4ABCDEFGHIJKLMNOPQRSTUVW XYZYXWVUTSRQPONMLKJIHGFEDCBA1 1 3 3 25 20 25 21 7 0 9 7 3 16 15 14 19 5 19 19 19 22 8 23 2 4 125 1 3 3 25 20 25 21 7 0 9 7 3 16 15 14 19 5 19 19 19 22 8 23 2 4 126 1 3 3 25 20 25 21 7 0 9 7 3 16 15 14 19 5 19 19 19 22 8 23 2 4 176 1 3 3 25 20 2 5 21 7 0 9 7 3 16 15 14 19 5 19 19 19 22 8 23 24 1

Sample Output

1620141414141414143783783780914733665286

*Hint*There is 7 palindromic substrings {"A", "a", "B", "C", "C", "D", "CDC"} in the first case. For the first person, the corresponding scores is {1, 1, 1, 1, 1, 1, 27}. For the second person, the corresponding scores is {25, 25, 24, 23, 23, 22, 620}.SourceChangchun Regional Contest problem solving: Spicy next door, into the pit! The range of k will exceed int, so it is better to use long long! I rewrote the code two times.

1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN =500010;4 Const intMoD =777777777;5 usingPII = pair<int,int>;6 usingLL =Long Long;7 intscore[ +][ -];8 PII D[MAXN];9LL b[maxn],k[ +];Ten structPalindromictree { One intch[maxn][ -],FAIL[MAXN],CNT[MAXN],LEN[MAXN],S[MAXN]; A inttot,last,n,m; -LL hs[maxn][ +]; - voidinit () { thetot = last = n =0; -NewNode0); -NewNode (-1); -fail[0] = fail[1] =1; +S[n] =-1; - } + intNewNodeintSlen =0) { Amemset (Ch[tot],0,sizeofCh[tot]); atmemset (Hs[tot],0,sizeofHs[tot]); -Fail[tot] = Cnt[tot] =0; -Len[tot] =Slen; - returntot++; - } - intGetfail (intx) { in while(S[n-len[x]-1] = S[n]) x =Fail[x]; - returnx; to } + voidExtendintc) { -S[++n] =C; the intCur =Getfail (last); * if(!Ch[cur][c]) { $ intnow = NewNode (Len[cur] +2);Panax NotoginsengFail[now] =Ch[getfail (Fail[cur])][c]; -CH[CUR][C] =Now ; the intid = (Len[cur] +1) >>1; + for(inti =0; I < m; ++i) AHs[now][i] = (Hs[cur][i] + b[id]*score[i][s[n])%MoD; the } +++cnt[last =Ch[cur][c]]; - } $ voidcount () { $ for(inti = tot-1; i >1; --i) -Cnt[fail[i]] + =Cnt[i]; - } the intSolveinti) { -LL tmp =0;Wuyi intSZ =0; the for(intj =2; J < tot; ++j) -d[sz++] = PII ((int) hs[j][i],cnt[j]); WuSort (d,d+sz); - for(intj =0; J < Sz; ++j) { AboutTMP + =D[j].second; $ if(tmp >= K[i])returnD[j].first; - } - } - } pt; A CharSTR[MAXN]; + intMain () { the intkase,n,m; - for(inti = b[0] =1; I <100010; ++i) B[i] = b[i-1]* -%MoD; $scanf"%d",&Kase); the while(kase--) { thescanf"%d%d%s",&n,&m,str); the pt.init (); thePT.M =m; - for(inti =0; I < m; ++i) { inscanf"%i64d", K +i); the for(intj =0; J < -; ++j) thescanf"%d", Score[i] +j); About } the for(inti =0; I < n; ++i) thePt.extend (str[i]-'a'); the Pt.count (); + for(inti =0; I < m; ++i) -printf"%d\n", Pt.solve (i)); thePutchar ('\ n');Bayi } the return 0; the } - /* - 3 the 2 1 the AB the 1-9 8 7 6 5 4 3 2 1 0-ten-A- the */

View Code

HDU 4426 palindromic Substring