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. Android开发笔记(一百四十三)任务调度JobScheduler

    Android开发笔记(一百四十三)任务调度JobScheduler

  2. 限制UITextField输入长度

    如果要限制UITextField输入长度最长不超过kMaxLength,那么需要实现做以下操作: 1.实现UITextFieldDelegate协议: 2.实现textField:shouldChan ...

  3. lambda表达式的简单入门

    前言:本人在看<Java核心技术I>的时候对lamdba表达式还不是太上心,只是当做一个Java 8的特性了解一下而已,可是在<Java核心技术II>里面多次用到,所以重新入门 ...

  4. dataSource' defined in class path resource [org/springframework/boot/autocon

    spring boot启动的时候抛出如下异常: dataSource' defined in class path resource [org/springframework/boot/autocon ...

  5. iOS 查看包架构信息

    lipo -info libUMSocial_Sdk_4.2.a 查看包架构信息

  6. CDN加速静态文件服务器的访问

    1.用于加速用户下载资源的速度. 简单来说,CDN相当于一个中间代理,原来我们需要请求某个网址比如www.baidu.com,请求会直接发送至百度的服务器上,假如请求者在新疆,但百度的服务器在北京,这 ...

  7. spfa模板+讲解

    zz http://blog.sina.com.cn/s/blog_6ad20aef0100mc1a.html Spfa算法 (模板源代码) 这是Bellman Ford的改进算法.    算法介绍: ...

  8. sstable, bigtable,leveldb,cassandra,hbase的lsm基础

    先看懂文献1和2 1. 先了解sstable.SSTable: Sorted String Table [2] [10] WiscKey:  类似myisam, key value分离, 根据ssd优 ...

  9. iOS之WKWebView

    Xcode8发布以后,编译器开始不支持IOS7,所以很多应用在适配IOS10之后都不在适配IOS7了,其中包括了很多大公司,网易新闻,滴滴出行等.因此,我们公司的应用也打算淘汰IOS7. 支持到IOS ...

  10. 【Java_Spring】控制反转IOC(Inversion of Control)

    1. IOC的概念 控制反转IoC(Inversion of Control)是一种设计思想,而DI(依赖注入)是实现IoC的一种方法.在没有使用IOC的程序中,对象间的依赖关系是靠硬编码的方式实现的 ...