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. 爬虫的两种解析方式 xpath和bs4

    1.xpath解析 from lxml import etree 两种方式使用:将html文档变成一个对象,然后调用对象的方法去查找指定的节点 (1)本地文件 tree = etree.parse(文 ...

  2. java 设计模式 之 桥梁模式

    桥梁模式:将抽象和实现解耦,使两者可以独立的变化.解释:将两个有组合关系,强耦合的对象,各自抽象然后解耦.(类关系图看https://www.cnblogs.com/blogxiao/p/951388 ...

  3. 电脑连接海信电视 HDMI

    注意:我们家的电视是海信的,所以不能代表所有的电视哦~~~ 家里电视有线电视已经过期很长时间了,早就想把电脑连接到电视上用电视做显示器的心了,今天来兴趣了,就弄了一下!!! 用电脑连接电视需要先解决两 ...

  4. kvc to nsdata

        [NSKeyedArchiver archivedDataWithRootObject:arr];    [NSKeyedUnarchiver unarchiveObjectWithData: ...

  5. 使用python查询天气

    python主代码 weather.py import urllib2 import json from city import city cityname = raw_input('你想查哪个城市的 ...

  6. 人人必知的10个 jQuery 小技巧

    原文地址:http://info.9iphp.com/10-jquery-tips-everyone-should-know/ 人人必知的10个 jQuery 小技巧   收集的10个 jQuery ...

  7. Ubuntu 16.04下Java环境安装与配置

    首先下载linux下的安装包 登陆网址https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.h ...

  8. 树形dp——覆盖所有边的最少费用(Protecting Zonk)

    一.问题描述 有一个n(n<=10000)个节点的无根树.有两种装置A,B,每种都有无限多个. 1.在某个节点X使用A装置需要C1(C1<=1000)的花费,并且此时与节点X相连的边都被覆 ...

  9. C语言中函数参数传递

    C语言中函数参数传递的三种方式 (1)值传递,就是把你的变量的值传递给函数的形式参数,实际就是用变量的值来新生成一个形式参数,因而在函数里对形参的改变不会影响到函数外的变量的值.(2)地址传递,就是把 ...

  10. XML解析(二) SAX解析

    XML解析之SAX解析: SAX解析器:SAXParser类同DOM一样也在javax.xml.parsers包下,此类的实例可以从 SAXParserFactory.newSAXParser() 方 ...