Palindromic Substring

Time Limit: 10000ms
Memory Limit: 65536KB

This problem will be judged on HDU. 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 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.
Kva 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

3
6 2
abcdca
3 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
7 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 0
4 10
zzzz
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 14
2 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 14
3 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 14
4 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 14
5 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 14
6 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 14
7 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 14
8 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 14
9 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 14
10 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 14
51 4
abcdefghijklmnopqrstuvwxyzyxwvutsrqponmlkjihgfedcba
1 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
25 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
26 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
76 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

1
620 14
14
14
14
14
14
14
378
378
378 0
9
14
733665286
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

 
解题:麻辣隔壁,入坑了!k的范围会超过int,所以用long long较好!害我重写了两遍代码
 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
const int mod = ;
using PII = pair<int,int>;
using LL = long long;
int score[][];
PII d[maxn];
LL B[maxn],k[];
struct PalindromicTree {
int ch[maxn][],fail[maxn],cnt[maxn],len[maxn],s[maxn];
int tot,last,n,m;
LL hs[maxn][];
void init() {
tot = last = n = ;
newnode();
newnode(-);
fail[] = fail[] = ;
s[n] = -;
}
int newnode(int slen = ) {
memset(ch[tot],,sizeof ch[tot]);
memset(hs[tot],,sizeof hs[tot]);
fail[tot] = cnt[tot] = ;
len[tot] = slen;
return tot++;
}
int getFail(int x) {
while(s[n - len[x] - ] != s[n]) x = fail[x];
return x;
}
void extend(int c) {
s[++n] = c;
int cur = getFail(last);
if(!ch[cur][c]) {
int now = newnode(len[cur] + );
fail[now] = ch[getFail(fail[cur])][c];
ch[cur][c] = now;
int id = (len[cur] + )>>;
for(int i = ; i < m; ++i)
hs[now][i] = (hs[cur][i] + B[id]*score[i][s[n]])%mod;
}
++cnt[last = ch[cur][c]];
}
void count() {
for(int i = tot-; i > ; --i)
cnt[fail[i]] += cnt[i];
}
int solve(int i){
LL tmp = ;
int sz = ;
for(int j = ; j < tot; ++j)
d[sz++] = PII((int)hs[j][i],cnt[j]);
sort(d,d+sz);
for(int j = ; j < sz; ++j){
tmp += d[j].second;
if(tmp >= k[i]) return d[j].first;
}
}
} pt;
char str[maxn];
int main() {
int kase,n,m;
for(int i = B[] = ; i < ; ++i) B[i] = B[i-]*%mod;
scanf("%d",&kase);
while(kase--) {
scanf("%d%d%s",&n,&m,str);
pt.init();
pt.m = m;
for(int i = ; i < m; ++i) {
scanf("%I64d",k + i);
for(int j = ; j < ; ++j)
scanf("%d",score[i] + j);
}
for(int i = ; i < n; ++i)
pt.extend(str[i]-'a');
pt.count();
for(int i = ; i < m; ++i)
printf("%d\n",pt.solve(i));
putchar('\n');
}
return ;
}
/*
3
2 1
ab
1 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 0
*/

HDU 4426 Palindromic Substring的更多相关文章

  1. 【HDOJ】4426 Palindromic Substring

    综合性很强的一道题目,结合manacher,后缀数组,哈希,RMQ,二分可解.基本思路是通过manacher可以找到所有可能的回文串,哈希去重,后缀数组二分找数目.最后暴力求解.需要注意kth需要为_ ...

  2. 最长回文子串-LeetCode 5 Longest Palindromic Substring

    题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  3. leetcode--5. Longest Palindromic Substring

    题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...

  4. [LeetCode] Longest Palindromic Substring 最长回文串

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  5. No.005:Longest Palindromic Substring

    问题: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

  6. Leetcode Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  7. 【leedcode】 Longest Palindromic Substring

    Given a , and there exists one unique longest palindromic substring. https://leetcode.com/problems/l ...

  8. [LeetCode_5] Longest Palindromic Substring

    LeetCode: 5. Longest Palindromic Substring class Solution { public: //动态规划算法 string longestPalindrom ...

  9. 5. Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

随机推荐

  1. 简单的鼠标经过特效-mouse事件

    <!doctype html> <html lang="zh-cn"> <head> <meta charset="UTF-8& ...

  2. WPF中获取指定坐标依赖对象数据项

    上图中红色框区域是一个自定义的ListBox控件,需要实现的功能是,点击红框区域中某项时,获取当前选中项的数据项 控件的MouseDown事件部分代码为: var x = TreeHelper.Fin ...

  3. 使用express4.x版、Jade模板以及mysql重写《nodejs开发指南》微博实例

    最近阅读<nodejs开发指南>一书,书是不错的,然而其微博代码示例用的是express3.x,用些过时了,运行代码出现不少bug(我电脑安的是express4.x),于是用express ...

  4. mac下相关操作命令

    查看端口使用情况 lsof -i tcp:

  5. Git常用命令的使用方法

    推荐一个比较好的GIT的教学地址,廖雪峰老师的git教程! 这里简述Git常用命令的使用方法: 一.初始化git 右键进入 Git Bash 1.建立身份信息 git config --global ...

  6. IOS拉伸之底盖设置

    1.选定拉伸 UIImageView *fieldImage=[[UIImageViewalloc]initWithFrame:CGRectMake(37,48+35,240, 32)]; field ...

  7. 浏览器对DIV+CSS兼容性问题大总结

    浏览器对DIV+CSS兼容性问题大总结 接触DIV+CSS架构已经快两年了,个人觉得css入门不难,但要学精并非一朝一夕的,现在大部分网络公司都比较主张用div+css来布局,这就面临着一个比较难的问 ...

  8. ajax传给springMVC数据编码集问题

    前台 ajax: $.ajax("${pageContext.request.contextPath}/hello",// 发送请求的URL字符串. { dataType : &q ...

  9. 电脑公司最新稳定win7系统下载

    系统来自系统妈:http://www.xitongma.com 系统概述 电脑公司ghost win7 x86(32位)万能装机版集成的软件符合电脑公司及电脑城装机绝大多数人要求及喜好,既大众,又时尚 ...

  10. HTML5与PHP的比较

    一:需求量比较 知名招聘网站拉勾网显示,北京地区HTML5的需求量只有73个,而PHP的需求量有500+个:智联招聘网显示,北京上海广州深圳HTML5的需求量是7475个,而PHP的需求量是12514 ...