HDU 4426 Palindromic Substring

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

2012 Asia ChangChun Regional Contest

解题:麻辣隔壁,入坑了!k的范围会超过int,所以用long long较好!害我重写了两遍代码

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 500010;
 4 const int mod = 777777777;
 5 using PII = pair<int,int>;
 6 using LL = long long;
 7 int score[21][26];
 8 PII d[maxn];
 9 LL B[maxn],k[21];
10 struct PalindromicTree {
11     int ch[maxn][26],fail[maxn],cnt[maxn],len[maxn],s[maxn];
12     int tot,last,n,m;
13     LL hs[maxn][21];
14     void init() {
15         tot = last = n = 0;
16         newnode(0);
17         newnode(-1);
18         fail[0] = fail[1] = 1;
19         s[n] = -1;
20     }
21     int newnode(int slen = 0) {
22         memset(ch[tot],0,sizeof ch[tot]);
23         memset(hs[tot],0,sizeof hs[tot]);
24         fail[tot] = cnt[tot] = 0;
25         len[tot] = slen;
26         return tot++;
27     }
28     int getFail(int x) {
29         while(s[n - len[x] - 1] != s[n]) x = fail[x];
30         return x;
31     }
32     void extend(int c) {
33         s[++n] = c;
34         int cur = getFail(last);
35         if(!ch[cur][c]) {
36             int now = newnode(len[cur] + 2);
37             fail[now] = ch[getFail(fail[cur])][c];
38             ch[cur][c] = now;
39             int id = (len[cur] + 1)>>1;
40             for(int i = 0; i < m; ++i)
41                 hs[now][i] = (hs[cur][i] + B[id]*score[i][s[n]])%mod;
42         }
43         ++cnt[last = ch[cur][c]];
44     }
45     void count() {
46         for(int i = tot-1; i > 1; --i)
47             cnt[fail[i]] += cnt[i];
48     }
49     int solve(int i){
50         LL tmp = 0;
51         int sz = 0;
52         for(int j = 2; j < tot; ++j)
53             d[sz++] = PII((int)hs[j][i],cnt[j]);
54         sort(d,d+sz);
55         for(int j = 0; j < sz; ++j){
56             tmp += d[j].second;
57             if(tmp >= k[i]) return d[j].first;
58         }
59     }
60 } pt;
61 char str[maxn];
62 int main() {
63     int kase,n,m;
64     for(int i = B[0] = 1; i < 100010; ++i) B[i] = B[i-1]*26%mod;
65     scanf("%d",&kase);
66     while(kase--) {
67         scanf("%d%d%s",&n,&m,str);
68         pt.init();
69         pt.m = m;
70         for(int i = 0; i < m; ++i) {
71             scanf("%I64d",k + i);
72             for(int j = 0; j < 26; ++j)
73                 scanf("%d",score[i] + j);
74         }
75         for(int i = 0; i < n; ++i)
76             pt.extend(str[i]-‘a‘);
77         pt.count();
78         for(int i = 0; i < m; ++i)
79             printf("%d\n",pt.solve(i));
80         putchar(‘\n‘);
81     }
82     return 0;
83 }
84 /*
85 3
86 2 1
87 ab
88 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
89 */

时间: 2024-08-08 09:41:00

HDU 4426 Palindromic Substring的相关文章

【HDOJ】4426 Palindromic Substring

综合性很强的一道题目,结合manacher,后缀数组,哈希,RMQ,二分可解.基本思路是通过manacher可以找到所有可能的回文串,哈希去重,后缀数组二分找数目.最后暴力求解.需要注意kth需要为__int64. 1 /* 4426 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #in

[string]Longest Palindromic Substring

Total Accepted: 82026 Total Submissions: 379898 Difficulty: Medium Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. Subscrib

LeetCode OJ:Longest Palindromic Substring(最长的回文字串)

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 玩了两天dota2,罪过罪过,还是应该老老实实刷题啊. 题目求得是最长的回文子串,这里使用的是暴力的解法,也就是解决两种回文"asdsa"以

5. Longest Palindromic Substring - Unsolved

https://leetcode.com/problems/longest-palindromic-substring/#/description Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example: Input: "babad" Output: "bab" Note: &

[Leetcode] Longest palindromic substring 最长回文子串

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 做这道题之前要先了解什么是回文子串.回文串通俗的解释是,分别从字符串两端开始遍历,得到的结果相同,如"abba",从两端的遍历结果都是:&q

【LeetCode-面试算法经典-Java实现】【05-Longest Palindromic Substring(最大回文字符串)】

背景 近期開始研究算法,于是在leetcode上做算法题,第五题Longest Palindromic Substring便是关于回文子串的. 什么是回文字串 回文字符串是指将该字符串前后颠倒之后和该字符串一样的字符串.比如:a,aaaa,aba,abba- 最长回文子串 要求最长回文子串,就须要遍历每个子串,时间复杂度是O(N2):推断字串是不是回文,时间复杂度是O(N),这种话算法的时间复杂度就是O(N3). 我刚開始想到的就是中心扩展法,代码例如以下: public static Stri

【leedcode】 Longest Palindromic Substring

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. https://leetcode.com/problems/longest-palindromic-substring/ 求最大回文的长度,其实这道题

5. Longest Palindromic Substring

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 思路:(beat 2.63%) 以一个位置为准,考察两侧相同距离是否相同.这样会导致"aaaaaaaaaaaaaaaaaaaaaaaaaa...&qu

LeetCode #5 Longest Palindromic Substring (M)

[Problem] Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. [Analysis] 这题的思路有很多种,网上也有各种讨论.这里我采用的是个人觉得比较好理解的一种利用Dynamic Progra