标签:
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
1 620 14 14 14 14 14 14 14 378 378 378 0 9 14 733665286
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 */
HDU 4426 Palindromic Substring
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4945894.html