HDU4426-Palindromic Substring

来源:互联网 发布:java shell 编辑:程序博客网 时间:2024/05/18 02:42

Palindromic Substring

                                                                     Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Others)
                                                                                                   Total Submission(s): 813    Accepted Submission(s): 182


Problem 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×263+4×262+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.
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 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}.
 

Source
2012 Asia ChangChun Regional Contest
 

Recommend
zhuyuanchen520
 


题意:给你一个字符串, 有m次询问, 第i次询问问的是在各个字母的权值的条件下权值第k小的回文串的权值是多少

解题思路:回文树,在建树时对所有询问进行统计


#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>#include <functional>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;const int maxn = 2e6 + 10;const LL mod = 777777777;char s[maxn];int n, m, a[25][30];LL p[maxn], k[30];struct node{int id;LL cnt, sum;bool operator<(const node &a)const{return sum < a.sum;}}b[maxn];struct PalindromicTree{const static int maxn = 2e6 + 10;int next[maxn][26], last, sz, tot;int fail[maxn], len[maxn];LL cnt[maxn], sum[maxn][30];char s[maxn];void Clear(){len[1] = -1; len[2] = 0;fail[2] = fail[1] = 1;last = (sz = 3) - 1;cnt[1] = cnt[2] = tot = 0;memset(next[1], 0, sizeof(next[1]));memset(next[2], 0, sizeof(next[2]));memset(sum[1], 0, sizeof sum[1]);memset(sum[2], 0, sizeof sum[2]);}int Node(int length){memset(next[sz], 0, sizeof(next[sz]));len[sz] = length;cnt[sz] = 1;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 (next[y][x]) { cnt[last = next[y][x]]++; return; }last = next[y][x] = Node(len[y] + 2);for (int i = 1; i <= m; i++) sum[last][i] = (sum[y][i] + p[(len[last] - 1) >> 1] * a[i][x]) % mod;fail[last] = len[last] == 1 ? 2 : next[getfail(fail[y])][x];}void work(){for (int i = sz - 1; i > 2; i--) cnt[fail[i]] += cnt[i];for (int i = 1; i <= m; i++){for (int j = 3; j < sz; j++) b[j] = { j,cnt[j],sum[j][i] };sort(b + 3, b + sz);for (int j = 3; j < sz; j++){if (k[i] > b[j].cnt) k[i] -= b[j].cnt;else { printf("%lld\n", b[j].sum); break; }}}printf("\n");}}solve;int main(){for (int i = p[0] = 1; i < 100005; i++) p[i] = p[i - 1] * 26 % mod;int t;scanf("%d", &t);while (t--){solve.Clear();scanf("%d%d%s", &n, &m, s);for (int i = 1; i <= m; i++){scanf("%lld", &k[i]);for (int j = 0; j < 26; j++) scanf("%d", &a[i][j]);}for (int i = 0; i < n; i++) solve.add(s[i]);solve.work();}return 0;}