HDU 4426 Palindromic Substring
Palindromic Substring
This problem will be judged on HDU. Original ID: 4426
64-bit integer IO format: %I64d Java class name: Main
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 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
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
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
#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的更多相关文章
- 【HDOJ】4426 Palindromic Substring
综合性很强的一道题目,结合manacher,后缀数组,哈希,RMQ,二分可解.基本思路是通过manacher可以找到所有可能的回文串,哈希去重,后缀数组二分找数目.最后暴力求解.需要注意kth需要为_ ...
- 最长回文子串-LeetCode 5 Longest Palindromic Substring
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- leetcode--5. Longest Palindromic Substring
题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...
- [LeetCode] Longest Palindromic Substring 最长回文串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- No.005:Longest Palindromic Substring
问题: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- Leetcode Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 【leedcode】 Longest Palindromic Substring
Given a , and there exists one unique longest palindromic substring. https://leetcode.com/problems/l ...
- [LeetCode_5] Longest Palindromic Substring
LeetCode: 5. Longest Palindromic Substring class Solution { public: //动态规划算法 string longestPalindrom ...
- 5. Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
随机推荐
- 删除所有约束、表、视图等SQL脚本
--删除所有约束.表.视图等SQL脚本 --############################################### --删除所有外键约束 --################# ...
- 【转】java的动态代理机制详解
java的动态代理机制详解 在学习Spring的时候,我们知道Spring主要有两大思想,一个是IoC,另一个就是AOP,对于IoC,依赖注入就不用多说了,而对于Spring的核心AOP来说,我们 ...
- activity间的传参
Intent有两个作用:激活组件和附带数据 激活另一个activity的方法显示意图: 1. Intent intent = new Intent(); intent.setClass ...
- uvm_sequence_item——sequence机制(一)
让子弹飞一会 UVM框架,将验证平台和激励分开,env以下属于平台部分,test和sequence属于激励,这样各司其职.我们可以将sequence_item 比喻成子弹,sequencer 类比成弹 ...
- java代码(生成long类型数字)
package test; public class GenerateNum { public static void main(String[] args) { //定义为long类型,需在数值后面 ...
- SQL 数学串函数
数学函数 ceiling 取上限 floor 取下限 round 四舍五入 len 长度 abs 绝对值 PI()圆周率 sqrt 开根号 qwuare 平方根 select 10 ...
- js数组去重方法包括Es6(方法有很多,但是需要考虑兼容性和数据类型场景)
1.Es6提供的方法 <script type="text/javascript"> //ES6里新添加了两个方法,set(set是一种新的数据结构,它可以接收一个数组 ...
- webpack前端构建工具学习总结(四)之自动化生成项目中的html页面
接续上文:webpack前端构建工具学习总结(三)之webpack.config.js配置文件 插件的介绍文档:https://www.npmjs.com/package/html-webpack-p ...
- Servlet Context
Servlet Context Container Provider 负责提供ServletContext的实现. A ServletContext is rooted at a known path ...
- SayLove微信小程序
目录 SayLove 表白墙微信小程序 程序结构 说明 程序效果图 配置过程 结语 云开发 quickstart 参考文档 SayLove 表白墙微信小程序 项目地址:https://github.c ...