#include<bits/stdc++.h> #define INF 0x3f3f3f3f #define maxn 500010 typedeflonglong ll; usingnamespacestd; #define rep(i, n, args...) for (ll i = 0, ##args; i < n; i++) #define repr(i, s, e, args...) for (ll i = s, ##args; i < e; i++) #define erg(i, u, args...) for (ll i = vtx[u], ##args; ~i; i = eg[i].nxt) #define fulset(x, v) memset(x, v, sizeof(x)); int exnxt[maxn]; int extend1[maxn]; int extend2[maxn]; char S[maxn]; char T[maxn]; voidex_GetNext() { int m = strlen(T); int a = 0, p = 0; exnxt[0] = m; for (int i = 1; i < m; i++) { if (i >= p || i + exnxt[i - a] >= p) { if (i >= p) p = i; while (p < m && T[p] == T[p - i]) p++; exnxt[i] = p - i; a = i; } else exnxt[i] = exnxt[i - a]; } }
/* 求解 extend[] */ voidGetExtend1() { int a = 0, p = 0; int n = strlen(S); int m = strlen(T); ex_GetNext(); for (int i = 0; i < n; i++) { if (i >= p || i + exnxt[i - a] >= p) // i >= p 的作用:举个典型例子,S 和 T 无一字符相同 { if (i >= p) p = i;
while (p < n && p - i < m && S[p] == T[p - i]) p++; extend1[i] = p - i; a = i; } else extend1[i] = exnxt[i - a]; } } voidGetExtend2() { int a = 0, p = 0; int m = strlen(S); int n = strlen(T); ex_GetNext(); for (int i = 0; i < n; i++) { if (i >= p || i + exnxt[i - a] >= p) // i >= p 的作用:举个典型例子,S 和 T 无一字符相同 { if (i >= p) p = i; while (p < n && p - i < m && T[p] == S[p - i]) p++; extend2[i] = p - i; a = i; } else extend2[i] = exnxt[i - a]; } } int v[30]; int sum[maxn]; intmain() { int t; scanf("%d",&t); while (t--) { rep(i, 26) { scanf("%d",&v[i]); } scanf("%s",S); int lens = strlen(S); sum[0] = v[S[0]-'a']; rep(i,lens) { T[lens-i-1] = S[i]; if (i != 0) sum[i] = v[S[i]-'a'] + sum[i-1]; } T[lens] = 0; GetExtend1(); GetExtend2(); int ans = max(v[S[0]-'a'] , v[S[lens-1]-'a']); repr(i,1,lens) { int temp = 0; if (extend2[lens - i] + lens -i ==lens) temp += sum[i-1]; if (extend1[i] + i == lens) temp += sum[lens-1] - sum[i-1]; ans = max(ans, temp); } cout << ans << endl; } return0; }