HDU 4426Palindromic Substring

来源:互联网 发布:4g手机网络变成e怎么办 编辑:程序博客网 时间:2024/06/10 06:43

Description

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 is 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 is M modulo 777,777,777. 
However, different person may have different values for 'a' to 'z'. For example, if 'a' is defined as 3, 'b' is defined as 1 and c is defined as 4, then the string "accbcca" has the score (3×26 3+4×26 2+4×26+1) modulo 777777777=55537. 
One day, a very long string S is discovered and everyone in the kingdom wants to know that among all the palindromic substrings of S, what the one with the K-th smallest score is. 
 

Input

The first line contains an integer T(1 ≤ T ≤ 20), the number of test cases. 
The first line in each case contains two integers n, m (1 ≤ n ≤ 100000, 1 ≤ m ≤ 20) where n is the length of S and m is the number of people in the kingdom. The second line is the string S consisting of only lowercase letters. The next m lines each containing 27 integers describes a person in the following format. 
i 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 K i-th smallest palindromic substring exists and va, vb, ..., vz are in the range of [0, 26). But the values may coincide. 
 

Output

For each person, output the score of the K-th smallest palindromic substring in one line. Print a blank line after each case.
 

Sample Input

36 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 04 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 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 145 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 147 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 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 4abcdefghijklmnopqrstuvwxyzyxwvutsrqponmlkjihgfedcba1 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 25 21 7 0 9 7 3 16 15 14 19 5 19 19 19 22 8 23 2 4 1
 

Sample Output

1620141414141414143783783780914733665286

Hint

There are 7 palindromic substrings {"a", "a", "b", "c", "c", "d", "cdc"} in the first case. For the first person, the corresponding scores are {1, 1, 1, 1, 1, 1, 27}. For the second person, the corresponding scores are {25, 25, 24, 23, 23, 22, 620}.


建立回文树然后直接在每个点上统计分数,最后排个序输出即可。

#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>using namespace std;typedef long long LL;const int low(int x) { return x&-x; }const int INF = 0x7FFFFFFF;const int mod = 777777777;const int maxn = 1e5 + 10;int T, n, m, c[20][26], now, ans[maxn];LL q[20], lg[maxn];LL cnt[maxn], score[maxn][20];char s[maxn];struct PalindromicTree{const static int maxn = 1e5 + 10;const static int size = 26;int next[maxn][size], last, sz, tot;int fail[maxn], len[maxn];char s[maxn];void clear(){len[1] = -1; len[2] = 0;fail[2] = fail[1] = 1;last = (sz = 3) - 2;tot = 0;memset(next[1], 0, sizeof(next[1]));memset(next[2], 0, sizeof(next[2]));memset(score[1], 0, sizeof(score[1]));memset(score[2], 0, sizeof(score[2]));}int Node(int length){memset(next[sz], 0, sizeof(next[sz]));len[sz] = length; cnt[sz] = 0; return sz++;}int getfail(int x){while (s[tot] != s[tot - len[x] - 1]) x = fail[x];return x;}void add(char pos){int x = (s[++tot] = pos) - 'a', y = getfail(last);if (!(last = next[y][x])){last = next[y][x] = Node(len[y] + 2);for (int i = 0; i < m; i++){score[last][i] = (score[y][i] + lg[len[last] - 1 >> 1] * c[i][x]) % mod;}fail[last] = len[last] == 1 ? 2 : next[getfail(fail[y])][x];}++cnt[last];}}solve;bool cmp(const int &x, const int &y){return score[x][now] < score[y][now];}void work(){int sz = solve.sz;for (int i = sz - 1; i > 2; i--) cnt[solve.fail[i]] += cnt[i];for (int i = 0; i < m; i++){for (int j = 3; j < sz; j++) ans[j] = j;now = i;std::sort(ans + 3, ans + sz, cmp);for (int j = 3; j < sz; j++){if (q[i] > cnt[ans[j]]) q[i] -= cnt[ans[j]];else { cout << score[ans[j]][i] << endl; break; }}}cout << endl;}int main(){scanf("%d", &T);while (T--){scanf("%d%d%s", &n, &m, s);for (int i = lg[0] = 1; i <= n; i++){lg[i] = lg[i - 1] * 26 % mod;}for (int i = 0; i < m; i++){cin >> q[i];for (int j = 0; j < 26; j++){scanf("%d", &c[i][j]);}}solve.clear();for (int i = 0; s[i]; i++) solve.add(s[i]);work();}return 0;}


0 0
原创粉丝点击